]> git.somenet.org - pub/jan/dst18.git/blob - ass2-service/auth/src/test/java/dst/ass2/service/auth/tests/AuthenticationServiceTest.java
Add template for assignment 2
[pub/jan/dst18.git] / ass2-service / auth / src / test / java / dst / ass2 / service / auth / tests / AuthenticationServiceTest.java
1 package dst.ass2.service.auth.tests;
2
3 import dst.ass1.jpa.model.IModelFactory;
4 import dst.ass1.jpa.model.IParticipant;
5 import dst.ass1.jpa.tests.TestData;
6 import dst.ass2.service.api.auth.AuthenticationException;
7 import dst.ass2.service.api.auth.NoSuchUserException;
8 import dst.ass2.service.auth.AuthenticationServiceApplication;
9 import dst.ass2.service.auth.ICachingAuthenticationService;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.junit.runner.RunWith;
13 import org.springframework.beans.BeansException;
14 import org.springframework.boot.test.context.SpringBootTest;
15 import org.springframework.context.ApplicationContext;
16 import org.springframework.context.ApplicationContextAware;
17 import org.springframework.test.context.ActiveProfiles;
18 import org.springframework.test.context.junit4.SpringRunner;
19
20 import javax.persistence.EntityManager;
21 import javax.persistence.PersistenceContext;
22 import javax.transaction.Transactional;
23 import java.security.MessageDigest;
24 import java.security.NoSuchAlgorithmException;
25
26 import static org.junit.Assert.*;
27
28 @RunWith(SpringRunner.class)
29 @SpringBootTest(classes = AuthenticationServiceApplication.class)
30 @Transactional
31 @ActiveProfiles("testdata")
32 public class AuthenticationServiceTest implements ApplicationContextAware {
33
34     @PersistenceContext
35     private EntityManager em;
36
37     private ApplicationContext applicationContext;
38
39     private IModelFactory modelFactory;
40     private ICachingAuthenticationService authenticationService;
41
42     @Override
43     public void setApplicationContext(ApplicationContext ctx) throws BeansException {
44         applicationContext = ctx;
45     }
46
47     @Before
48     public void setUp() {
49         modelFactory = applicationContext.getBean(IModelFactory.class);
50         authenticationService = applicationContext.getBean(ICachingAuthenticationService.class);
51
52         // reload the data before each test
53         authenticationService.loadData();
54     }
55
56     @Test
57     public void authenticate_existingUser_createsTokenCorrectly() throws Exception {
58         String token = authenticationService.authenticate(TestData.PARTICIPANT_1_EMAIL, TestData.PARTICIPANT_1_PW);
59         assertNotNull(token);
60     }
61
62     @Test
63     public void authenticate_existingUserNotInCache_createsTokenCorrectly() throws Exception {
64         IParticipant p = modelFactory.createParticipant();
65
66         p.setEmail("non-cached@example.com");
67         try {
68             p.setPassword(MessageDigest.getInstance("SHA1").digest("somepw".getBytes()));
69         } catch (NoSuchAlgorithmException e) {
70             throw new RuntimeException(e);
71         }
72         p.setName("non-cached");
73         p.setAccountNo("accountno");
74         p.setBankCode("bankcode");
75
76         em.persist(p);
77
78         String token = authenticationService.authenticate(p.getEmail(), "somepw");
79         assertNotNull(token);
80     }
81
82     @Test(expected = NoSuchUserException.class)
83     public void authenticate_invalidUser_throwsException() throws Exception {
84         authenticationService.authenticate("nonexisting@example.com", "foo");
85     }
86
87     @Test(expected = AuthenticationException.class)
88     public void authenticate_invalidPassword_throwsException() throws Exception {
89         authenticationService.authenticate(TestData.PARTICIPANT_1_EMAIL, "foo");
90     }
91
92     @Test
93     public void changePassword_existingUser_passwordChanged() throws Exception {
94         authenticationService.changePassword(TestData.PARTICIPANT_1_EMAIL, "newPwd");
95         assertNotNull(authenticationService.authenticate(TestData.PARTICIPANT_1_EMAIL, "newPwd"));
96     }
97
98     @Test(expected = NoSuchUserException.class)
99     public void changePassword_nonExistingUser_throwsException() throws Exception {
100         authenticationService.changePassword("nonexisting@example.com", "foo");
101     }
102
103     @Test
104     public void getUser_existingToken_returnsUser() throws Exception {
105         String token = authenticationService.authenticate(TestData.PARTICIPANT_1_EMAIL, TestData.PARTICIPANT_1_PW);
106         assertEquals(TestData.PARTICIPANT_1_EMAIL, authenticationService.getUser(token));
107     }
108
109     @Test
110     public void getUser_nonExistingToken_returnsNull() throws Exception {
111         assertNull(authenticationService.getUser("invalidToken"));
112     }
113
114     @Test
115     public void isValid_existingToken_returnsTrue() throws Exception {
116         String token = authenticationService.authenticate(TestData.PARTICIPANT_1_EMAIL, TestData.PARTICIPANT_1_PW);
117         assertTrue(authenticationService.isValid(token));
118     }
119
120     @Test
121     public void isValid_nonExistingToken_returnsFalse() throws Exception {
122         assertFalse(authenticationService.isValid("invalidToken"));
123     }
124
125     @Test
126     public void invalidate_validToken_tokenInvalidatedReturnsTrue() throws Exception {
127         String token = authenticationService.authenticate(TestData.PARTICIPANT_1_EMAIL, TestData.PARTICIPANT_1_PW);
128         assertTrue(authenticationService.invalidate(token));
129         assertFalse(authenticationService.isValid(token));
130         assertNull(authenticationService.getUser(token));
131     }
132
133     @Test
134     public void invalidate_invalidToken_returnsFalse() throws Exception {
135         assertFalse(authenticationService.invalidate("invalidToken"));
136     }
137
138 }