Skip to content

Commit

Permalink
fix : mediator add to alias should not store duplicated alias (#19)
Browse files Browse the repository at this point in the history
* add to alias is a set, and not duplicates fixed ussing mongo addtoset

* Added tests and Updated the reposne to nochnage in case of the noupdate was done

---------

Co-authored-by: Shailesh Patil <[email protected]>
Signed-off-by: Shailesh Patil <[email protected]>

Signed-off-by: Shailesh Patil <[email protected]>
  • Loading branch information
shaileshp0110 authored and mineme0110 committed May 1, 2024
1 parent d9c6926 commit a9cdd03
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ class UserAccountRepo(reactiveMongoApi: ReactiveMongoApi)(using ec: ExecutionCon

}

def addAlias(owner: DIDSubject, newAlias: DIDSubject): ZIO[Any, StorageError, Either[String, Unit]] = {
def addAlias(owner: DIDSubject, newAlias: DIDSubject): ZIO[Any, StorageError, Either[String, Int]] = {
def selector: BSONDocument = BSONDocument("did" -> owner)

def update: BSONDocument = BSONDocument(
"$push" -> BSONDocument(
"$addToSet" -> BSONDocument(
"alias" -> newAlias
)
)
Expand All @@ -85,11 +85,11 @@ class UserAccountRepo(reactiveMongoApi: ReactiveMongoApi)(using ec: ExecutionCon
)
.tapError(err => ZIO.logError(s"addAlias : ${err.getMessage}"))
.mapError(ex => StorageThrowable(ex))
} yield Right(())
} yield Right(result.nModified)

}

def removeAlias(owner: DIDSubject, newAlias: DIDSubject): ZIO[Any, StorageError, Either[String, Unit]] = {
def removeAlias(owner: DIDSubject, newAlias: DIDSubject): ZIO[Any, StorageError, Either[String, Int]] = {
def selector: BSONDocument = BSONDocument("did" -> owner)
def update: BSONDocument = BSONDocument(
"$pull" -> BSONDocument(
Expand All @@ -107,7 +107,7 @@ class UserAccountRepo(reactiveMongoApi: ReactiveMongoApi)(using ec: ExecutionCon
)
.tapError(err => ZIO.logError(s"removeAlias : ${err.getMessage}"))
.mapError(ex => StorageThrowable(ex))
} yield Right(())
} yield Right(result.nModified)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ object MediatorCoordinationExecuter extends ProtocolExecuterWithServices[Protoco
case (fromto, KeylistAction.add) =>
repo.addAlias(m.from.toDID, fromto.toDID).map {
case Left(value) => (fromto, KeylistAction.add, KeylistResult.server_error)
case Right(0) => (fromto, KeylistAction.add, KeylistResult.no_change)
case Right(newState) => (fromto, KeylistAction.add, KeylistResult.success)
}
case (fromto, KeylistAction.remove) =>
repo.removeAlias(m.from.toDID, fromto.toDID).map {
case Left(value) => (fromto, KeylistAction.remove, KeylistResult.server_error)
case Right(0) => (fromto, KeylistAction.remove, KeylistResult.no_change)
case Right(newState) => (fromto, KeylistAction.remove, KeylistResult.success)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,53 @@ object UserAccountRepoSpec extends ZIOSpecDefault with AccountStubSetup {
assertTrue(result.isEmpty)
}
},
test("Add alias to existing Did Account return right") {
test("Add alias to existing Did Account return right nModified value 1") {
for {
userAccount <- ZIO.service[UserAccountRepo]
result <- userAccount.addAlias(DIDSubject(alice), DIDSubject(bob))
didAccount <- userAccount.getDidAccount(DIDSubject(alice))
} yield {
assertTrue(result.isRight)
assertTrue(result == Right(()))
assertTrue(result == Right((1)))
assertTrue(didAccount.isDefined)
val alias: Seq[String] = didAccount.map(_.alias.map(_.did)).getOrElse(Seq.empty)
assertTrue(alias == Seq(alice, bob))
}
},
test("Remove alias to existing Did Account should return right") {
test("Add same alias to existing Did Account return right with nModified value 0") {
for {
userAccount <- ZIO.service[UserAccountRepo]
result <- userAccount.addAlias(DIDSubject(alice), DIDSubject(alice))
didAccount <- userAccount.getDidAccount(DIDSubject(alice))
} yield {
assertTrue(result.isRight)
assertTrue(result == Right(0))
assertTrue(didAccount.isDefined)
val alias: Seq[String] = didAccount.map(_.alias.map(_.did)).getOrElse(Seq.empty)
assertTrue(alias == Seq(alice, bob))
}
},
test("Remove alias to existing Did Account should return right with nModified value 1 ") {
for {
userAccount <- ZIO.service[UserAccountRepo]
result <- userAccount.removeAlias(DIDSubject(alice), DIDSubject(bob))
didAccount <- userAccount.getDidAccount(DIDSubject(alice))
} yield {
assertTrue(result.isRight)
assertTrue(result == Right(1))
assertTrue(didAccount.isDefined)
val alias: Seq[String] = didAccount.map(_.alias.map(_.did)).getOrElse(Seq.empty)
assertTrue(alias == Seq(alice))
}
},
test("Remove alias to unknown or unregister alias Did should return right with noModified value 0") {
for {
userAccount <- ZIO.service[UserAccountRepo]
result <- userAccount.removeAlias(DIDSubject(alice), DIDSubject(bob))
didAccount <- userAccount.getDidAccount(DIDSubject(alice))
} yield {
assertTrue(result.isRight)
assertTrue(result == Right(()))
assertTrue(result == Right(0))
assertTrue(didAccount.isDefined)
val alias: Seq[String] = didAccount.map(_.alias.map(_.did)).getOrElse(Seq.empty)
assertTrue(alias == Seq(alice))
Expand All @@ -98,7 +124,7 @@ object UserAccountRepoSpec extends ZIOSpecDefault with AccountStubSetup {
didAccount <- userAccount.getDidAccount(DIDSubject(alice))
} yield {
assertTrue(result.isRight)
assertTrue(result == Right(()))
assertTrue(result == Right(1))
assertTrue(msgAdded.writeErrors == Nil)
assertTrue(msgAdded.n == 1)
assertTrue(addedToInbox == 1)
Expand Down

0 comments on commit a9cdd03

Please sign in to comment.