33import com .nexblocks .authguard .dal .hibernate .common .ReactiveQueryExecutor ;
44import com .nexblocks .authguard .dal .hibernate .common .SessionProvider ;
55import com .nexblocks .authguard .dal .model .*;
6- import com .nexblocks .authguard .service .exceptions .ServiceConflictException ;
76import org .junit .jupiter .api .BeforeAll ;
87import org .junit .jupiter .api .Test ;
98import org .junit .jupiter .api .TestInstance ;
109
1110import java .time .Instant ;
12- import java .util .Collections ;
13- import java .util .List ;
14- import java .util .Optional ;
15- import java .util .UUID ;
11+ import java .util .*;
1612import java .util .stream .Collectors ;
1713
1814import static org .assertj .core .api .Assertions .assertThat ;
2117@ TestInstance (TestInstance .Lifecycle .PER_CLASS )
2218public class HibernateAccountsRepositoryTest {
2319 private HibernateAccountsRepository repository ;
20+ private HibernatePermissionsRepository permissionsRepository ;
2421 protected HibernateUserIdentifiersRepository userIdentifiersRepository ;
2522
2623 @ BeforeAll
@@ -36,6 +33,7 @@ protected void initialize(final SessionProvider sessionProvider) {
3633
3734 repository = new HibernateAccountsRepository (queryExecutor );
3835 userIdentifiersRepository = new HibernateUserIdentifiersRepository (queryExecutor );
36+ permissionsRepository = new HibernatePermissionsRepository (queryExecutor );
3937 }
4038
4139 @ Test
@@ -56,6 +54,7 @@ public void saveAndGetById() {
5654 UserIdentifierDO .builder ()
5755 .identifier (email .getEmail ())
5856 .type (UserIdentifierDO .Type .EMAIL )
57+ .active (true )
5958 .build ()
6059 ))
6160 .build ();
@@ -86,6 +85,7 @@ public void getByExternalId() {
8685 UserIdentifierDO .builder ()
8786 .identifier (email .getEmail ())
8887 .type (UserIdentifierDO .Type .EMAIL )
88+ .active (true )
8989 .build ()
9090 ))
9191 .build ();
@@ -116,6 +116,7 @@ public void getByEmail() {
116116 UserIdentifierDO .builder ()
117117 .identifier (email .getEmail ())
118118 .type (UserIdentifierDO .Type .EMAIL )
119+ .active (true )
119120 .build ()
120121 ))
121122 .build ();
@@ -141,6 +142,7 @@ public void findByIdentifier() {
141142 .identifier (identifier )
142143 .type (UserIdentifierDO .Type .USERNAME )
143144 .domain ("main" )
145+ .active (true )
144146 .build ()))
145147 .hashedPassword (PasswordDO .builder ()
146148 .password ("password" )
@@ -231,6 +233,7 @@ public void updatePassword() {
231233 .identifiers (Collections .singleton (UserIdentifierDO .builder ()
232234 .identifier (identifier )
233235 .type (UserIdentifierDO .Type .USERNAME )
236+ .active (true )
234237 .build ()))
235238 .hashedPassword (PasswordDO .builder ()
236239 .password ("password" )
@@ -275,10 +278,13 @@ public void removeIdentifier() {
275278 .roles (Collections .emptySet ())
276279 .permissions (Collections .emptySet ())
277280 .metadata (Collections .emptyMap ())
278- .identifiers (Collections .singleton (UserIdentifierDO .builder ()
279- .identifier (identifier )
280- .type (UserIdentifierDO .Type .USERNAME )
281- .build ()))
281+ .identifiers (new HashSet <>(Arrays .asList (
282+ UserIdentifierDO .builder ()
283+ .identifier (identifier )
284+ .type (UserIdentifierDO .Type .USERNAME )
285+ .active (true )
286+ .build ()
287+ )))
282288 .hashedPassword (PasswordDO .builder ()
283289 .password ("password" )
284290 .salt ("salt" )
@@ -332,6 +338,7 @@ public void updateIdentifier() {
332338 .identifiers (Collections .singleton (UserIdentifierDO .builder ()
333339 .identifier (identifier )
334340 .type (UserIdentifierDO .Type .USERNAME )
341+ .active (true )
335342 .build ()))
336343 .hashedPassword (PasswordDO .builder ()
337344 .password ("password" )
@@ -589,4 +596,76 @@ public void saveDuplicateNullPhoneNumbers() {
589596 repository .save (first ).subscribeAsCompletionStage ().join ();
590597 repository .save (second ).subscribeAsCompletionStage ().join ();
591598 }
599+
600+ @ Test
601+ public void addAndRemoveAccountPermission () {
602+ long id = Math .abs (UUID .randomUUID ().getMostSignificantBits ());
603+ String identifier = "addAccountPermission" ;
604+
605+ AccountDO account = AccountDO .builder ()
606+ .id (id )
607+ .roles (Collections .emptySet ())
608+ .permissions (Collections .emptySet ())
609+ .metadata (Collections .emptyMap ())
610+ .domain ("main" )
611+ .identifiers (Collections .singleton (UserIdentifierDO .builder ()
612+ .identifier (identifier )
613+ .type (UserIdentifierDO .Type .USERNAME )
614+ .domain ("main" )
615+ .active (true )
616+ .build ()))
617+ .hashedPassword (PasswordDO .builder ()
618+ .password ("password" )
619+ .salt ("salt" )
620+ .build ())
621+ .build ();
622+
623+ AccountDO persisted = repository .save (account ).subscribeAsCompletionStage ().join ();
624+
625+ PermissionDO firstPermission = permissionsRepository .save (
626+ PermissionDO .builder ()
627+ .id (Math .abs (UUID .randomUUID ().getMostSignificantBits ()))
628+ .domain ("main" )
629+ .forAccounts (true )
630+ .permissionGroup ("account_tests" )
631+ .name ("read" )
632+ .build ()
633+ )
634+ .subscribeAsCompletionStage ()
635+ .join ();
636+
637+ PermissionDO secondPermission = permissionsRepository .save (
638+ PermissionDO .builder ()
639+ .id (Math .abs (UUID .randomUUID ().getMostSignificantBits ()))
640+ .domain ("main" )
641+ .forAccounts (true )
642+ .permissionGroup ("account_tests" )
643+ .name ("write" )
644+ .build ()
645+ )
646+ .subscribeAsCompletionStage ()
647+ .join ();
648+
649+ // add
650+ repository .addAccountPermissions (persisted , Arrays .asList (firstPermission , secondPermission ))
651+ .subscribeAsCompletionStage ()
652+ .join ();
653+
654+ Optional <AccountDO > afterAdd = repository .getById (persisted .getId ()).subscribeAsCompletionStage ().join ();
655+
656+ assertThat (afterAdd ).isNotEmpty ();
657+ assertThat (afterAdd .get ().getPermissions ())
658+ .containsExactlyInAnyOrder (firstPermission , secondPermission );
659+
660+ // remove one
661+ repository .removeAccountPermissions (persisted , Collections .singletonList (firstPermission ))
662+ .subscribeAsCompletionStage ()
663+ .join ();
664+
665+ Optional <AccountDO > afterRemove = repository .getById (persisted .getId ()).subscribeAsCompletionStage ().join ();
666+
667+ assertThat (afterRemove ).isNotEmpty ();
668+ assertThat (afterRemove .get ().getPermissions ())
669+ .containsExactlyInAnyOrder (secondPermission );
670+ }
592671}
0 commit comments