Clover icon

sunshower-sdk

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

File DefaultSecurityEndpointTest.java

 

Code metrics

0
21
6
1
119
102
6
0.29
3.5
6
1

Classes

Class Line # Actions
DefaultSecurityEndpointTest 35 21 6
1.0100%
 

Contributing tests

This file is covered by 12 tests. .

Source view

1    package io.sunshower.sdk.v1.security;
2   
3    import io.sunshower.core.security.crypto.EncryptionService;
4    import io.sunshower.model.core.auth.Role;
5    import io.sunshower.model.core.auth.User;
6    import io.sunshower.sdk.test.SdkTest;
7    import io.sunshower.sdk.test.TestRoles;
8    import io.sunshower.sdk.v1.endpoints.core.security.SecurityEndpoint;
9    import io.sunshower.sdk.v1.endpoints.core.security.SignupEndpoint;
10    import io.sunshower.sdk.v1.model.core.security.*;
11    import io.sunshower.test.ws.Remote;
12    import org.junit.jupiter.api.BeforeEach;
13    import org.junit.jupiter.api.Test;
14    import org.springframework.test.annotation.DirtiesContext;
15    import org.springframework.test.context.jdbc.Sql;
16   
17    import javax.inject.Inject;
18    import javax.persistence.EntityManager;
19    import javax.persistence.PersistenceContext;
20    import javax.transaction.NotSupportedException;
21    import javax.transaction.SystemException;
22    import javax.ws.rs.BadRequestException;
23    import javax.ws.rs.NotAuthorizedException;
24    import javax.ws.rs.NotFoundException;
25    import java.util.Arrays;
26    import java.util.HashSet;
27    import java.util.Set;
28    import java.util.stream.Collectors;
29   
30    import static org.hamcrest.CoreMatchers.*;
31    import static org.junit.Assert.assertThat;
32    import static org.junit.jupiter.api.Assertions.assertThrows;
33   
34    @Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = "classpath:/sql/drop-roles.sql")
 
35    public class DefaultSecurityEndpointTest extends SdkTest {
36   
37    @PersistenceContext
38    private EntityManager entityManager;
39   
40    @Remote
41    private SecurityEndpoint securityEndpoint;
42   
43    @Remote
44    private SignupEndpoint endpoint;
45   
46    @Inject
47    private EncryptionService encryptionService;
48   
 
49  7 toggle @Test
50    @BeforeEach
51    public void ensureSecurityEndpointIsInjected() {
52  7 assertThat(securityEndpoint, is(not(nullValue())));
53    }
54   
 
55  1 toggle @Test
56    public void ensureSecurityEndpointThrowsCorrectExceptionWhenProvidedNullToken() {
57  1 assertThrows(
58    BadRequestException.class,
59    () -> {
60  1 securityEndpoint.authenticate((AuthenticationTokenElement) null);
61    });
62    }
63   
 
64  1 toggle @Test
65    public void ensureAuthenticateThrows405WhenProvidedNullTokenComponent() {
66  1 AuthenticationTokenElement element = new AuthenticationTokenElement(null);
67  1 assertThrows(
68    NotAuthorizedException.class,
69    () -> {
70  1 securityEndpoint.authenticate(element);
71    });
72    }
73   
 
74  1 toggle @Test
75    public void ensureAuthenticatingWithInvalidUsernameProducesSaneException() {
76  1 assertThrows(
77    NotFoundException.class,
78    () -> {
79  1 Authenticate.as(null).withPassword("frapper").at(securityEndpoint);
80    });
81    }
82   
 
83  1 toggle @Test
84    public void ensureLoggingInAsExistingUserProducesToken() {
85  1 withPrincipals(TestRoles.administrator1())
86    .perform(
87    () -> {
88  1 AuthenticationElement result =
89    Authenticate.as("administrator1").withPassword("frapadap1").at(securityEndpoint);
90  1 assertThat(result, is(not(nullValue())));
91  1 assertThat(result.getToken(), is(not(nullValue())));
92  1 securityEndpoint.validate(result.getToken());
93    });
94    }
95   
 
96  1 toggle @Test
97    public void ensureAuthenticatingViaTokenProducesPrincipalWithCorrectRoles() {
98  1 withPrincipals(TestRoles.administrator1())
99    .perform(
100    () -> {
101  1 AuthenticationElement result =
102    Authenticate.as("administrator1").withPassword("frapadap1").at(securityEndpoint);
103   
104  1 final PrincipalElement principalElement = result.getPrincipal();
105   
106  1 assertThat(principalElement, is(not(nullValue())));
107  1 assertThat(principalElement.getUsername(), is("administrator1"));
108  1 assertThat(principalElement.getRoles().size(), is(3));
109  1 Set<String> roleNames =
110    principalElement
111    .getRoles()
112    .stream()
113    .map(RoleElement::getAuthority)
114    .collect(Collectors.toSet());
115  1 assertThat(roleNames, is(new HashSet<>(Arrays.asList("admin", "tenant:user", "tenant:admin"))));
116    });
117    }
118   
119    }