Clover icon

sunshower-sdk

  1. Project Clover database Wed Apr 4 2018 21:39:43 UTC
  2. Package io.sunshower.sdk.v1.core.security

File DefaultUserEndpointTest.java

 

Code metrics

2
43
10
1
168
150
11
0.26
4.3
10
1.1

Classes

Class Line # Actions
DefaultUserEndpointTest 29 43 11
0.981818298.2%
 

Contributing tests

This file is covered by 9 tests. .

Source view

1    package io.sunshower.sdk.v1.core.security;
2   
3    import io.sunshower.model.core.auth.Role;
4    import io.sunshower.model.core.auth.User;
5    import io.sunshower.sdk.core.ActivationEndpoint;
6    import io.sunshower.sdk.lang.IdentifierElement;
7    import io.sunshower.sdk.test.SdkTest;
8    import io.sunshower.sdk.v1.endpoints.core.security.SignupEndpoint;
9    import io.sunshower.sdk.v1.endpoints.core.security.UserEndpoint;
10    import io.sunshower.sdk.v1.model.core.security.PrincipalElement;
11    import io.sunshower.sdk.v1.model.core.security.RegistrationRequestElement;
12    import io.sunshower.service.security.PermissionsService;
13    import io.sunshower.test.ws.Remote;
14    import org.jetbrains.annotations.NotNull;
15    import org.junit.jupiter.api.Test;
16   
17    import javax.inject.Inject;
18    import javax.persistence.EntityManager;
19    import javax.persistence.PersistenceContext;
20    import javax.ws.rs.ForbiddenException;
21    import javax.ws.rs.NotAuthorizedException;
22    import java.util.List;
23   
24    import static io.sunshower.sdk.test.TestRoles.administrator;
25    import static org.hamcrest.CoreMatchers.*;
26    import static org.junit.Assert.assertThat;
27    import static org.junit.jupiter.api.Assertions.assertThrows;
28   
 
29    public class DefaultUserEndpointTest extends SdkTest {
30   
31    @Inject private PermissionsService<?> permissionsService;
32   
33    @Remote private UserEndpoint userEndpoint;
34    @Remote private SignupEndpoint signupEndpoint;
35    @Remote private ActivationEndpoint activationEndpoint;
36   
37    @PersistenceContext private EntityManager entityManager;
38   
 
39  1 toggle @Test
40    public void ensureListingActiveUsersReturnsEmptyListWhenNoUsersExist() {
41  1 withPrincipals(administrator())
42    .perform(
43    () -> {
44  1 changeSession("administrator");
45  1 assertThat(userEndpoint.list(true).size(), is(1));
46    });
47    }
48   
49    IdentifierElement id;
50   
 
51  1 toggle @Test
52    public void ensureSavingAndDeletingUserWorks() {
53  1 try {
54  1 RegistrationRequestElement registrationRequestElement =
55    RegistrationRequestElement.newRegistration()
56    .firstName("wab")
57    .lastName("dab")
58    .username("wabbab")
59    .phoneNumber("970-888-8888")
60    .emailAddress("coolbean")
61    .password("coolbeans")
62    .create();
63  1 signupEndpoint.signup(registrationRequestElement);
64  1 permissionsService.impersonate(
65    () -> {
66  1 List<RegistrationRequestElement> list = signupEndpoint.list();
67  1 assertThat(list.size(), is(1));
68  1 id = signupEndpoint.approve(list.get(0).getRegistrationId());
69    },
70    new Role("admin"));
71    } finally {
72  1 if (id != null) {
73  1 permissionsService.impersonate(() -> userEndpoint.delete(id.getValue()), new Role("admin"));
74    }
75    }
76    }
77   
 
78  1 toggle @Test
79    public void ensureListingActiveUsersWhileUnauthorizedProduces401() {
80  1 assertThrows(
81    NotAuthorizedException.class,
82    () -> {
83  1 assertThat(userEndpoint.list(true).size(), is(2));
84    });
85    }
86   
 
87  1 toggle @Test
88    public void ensureListingActiveUsersFailsWithUnauthorizedWhenAuthenticatedAsTenantUser() {
89  1 assertThrows(
90    ForbiddenException.class,
91    () -> {
92  1 permissionsService.impersonate(
93    () -> {
94  1 assertThat(userEndpoint.list(true).size(), is(2));
95    },
96    new Role("tenant:user"));
97    });
98    }
99   
 
100  1 toggle @Test
101    public void ensureNoInactiveUsersAppearInActiveUserList() {
102  1 withPrincipals(administrator())
103    .perform(
104    () -> {
105  1 changeSession("administrator");
106  1 final User inactiveUser = createInactiveUser();
107  1 entityManager.persist(inactiveUser);
108  1 entityManager.flush();
109  1 List<PrincipalElement> list = userEndpoint.list(true);
110  1 System.out.println(list);
111  1 assertThat(
112    userEndpoint.list(true).stream().allMatch(PrincipalElement::isActive), is(true));
113    });
114    }
115   
 
116  1 toggle @NotNull
117    private User createInactiveUser() {
118  1 final User inactiveUser = new User();
119  1 inactiveUser.setActive(false);
120  1 inactiveUser.setUsername("inactiveBlorper");
121  1 inactiveUser.setPassword(password("inactiveBlorper", "password"));
122  1 inactiveUser.getDetails().setEmailAddress("inactive@blorper.com");
123  1 return inactiveUser;
124    }
125   
 
126  1 toggle @Test
127    public void ensureActiveUserReturnsWithRelevantDetails() {
128  1 withPrincipals(administrator())
129    .perform(
130    () -> {
131  1 changeSession("administrator");
132  1 final PrincipalElement principal = userEndpoint.list(true).get(0);
133  1 assertThat(principal.getEmailAddress(), is(not(nullValue())));
134    });
135    }
136   
 
137  1 toggle @Test
138    public void ensureListingInactiveUsersReturnsEmptyListWhenNoUsersExist() {
139  1 withPrincipals(administrator())
140    .perform(
141    () -> {
142  1 changeSession("administrator");
143  1 assertThat(userEndpoint.list(false).size(), is(0));
144    });
145    }
146   
 
147  1 toggle @Test
148    public void ensureListingInactiveUsersWhileUnauthorizedProduces401() {
149  1 assertThrows(
150    NotAuthorizedException.class,
151    () -> {
152  1 assertThat(userEndpoint.list(false).size(), is(0));
153    });
154    }
155   
 
156  1 toggle @Test
157    public void ensureListingInactiveUsersFailsWithUnauthorizedWhenAuthenticatedAsTenantUser() {
158  1 assertThrows(
159    ForbiddenException.class,
160    () -> {
161  1 permissionsService.impersonate(
162    () -> {
163  1 assertThat(userEndpoint.list(false).size(), is(0));
164    },
165    new Role("tenant:user"));
166    });
167    }
168    }