1 package dst.ass2.service.auth.tests;
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;
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;
26 import static org.junit.Assert.*;
28 @RunWith(SpringRunner.class)
29 @SpringBootTest(classes = AuthenticationServiceApplication.class)
31 @ActiveProfiles("testdata")
32 public class AuthenticationServiceTest implements ApplicationContextAware {
35 private EntityManager em;
37 private ApplicationContext applicationContext;
39 private IModelFactory modelFactory;
40 private ICachingAuthenticationService authenticationService;
43 public void setApplicationContext(ApplicationContext ctx) throws BeansException {
44 applicationContext = ctx;
49 modelFactory = applicationContext.getBean(IModelFactory.class);
50 authenticationService = applicationContext.getBean(ICachingAuthenticationService.class);
52 // reload the data before each test
53 authenticationService.loadData();
57 public void authenticate_existingUser_createsTokenCorrectly() throws Exception {
58 String token = authenticationService.authenticate(TestData.PARTICIPANT_1_EMAIL, TestData.PARTICIPANT_1_PW);
63 public void authenticate_existingUserNotInCache_createsTokenCorrectly() throws Exception {
64 IParticipant p = modelFactory.createParticipant();
66 p.setEmail("non-cached@example.com");
68 p.setPassword(MessageDigest.getInstance("SHA1").digest("somepw".getBytes()));
69 } catch (NoSuchAlgorithmException e) {
70 throw new RuntimeException(e);
72 p.setName("non-cached");
73 p.setAccountNo("accountno");
74 p.setBankCode("bankcode");
78 String token = authenticationService.authenticate(p.getEmail(), "somepw");
82 @Test(expected = NoSuchUserException.class)
83 public void authenticate_invalidUser_throwsException() throws Exception {
84 authenticationService.authenticate("nonexisting@example.com", "foo");
87 @Test(expected = AuthenticationException.class)
88 public void authenticate_invalidPassword_throwsException() throws Exception {
89 authenticationService.authenticate(TestData.PARTICIPANT_1_EMAIL, "foo");
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"));
98 @Test(expected = NoSuchUserException.class)
99 public void changePassword_nonExistingUser_throwsException() throws Exception {
100 authenticationService.changePassword("nonexisting@example.com", "foo");
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));
110 public void getUser_nonExistingToken_returnsNull() throws Exception {
111 assertNull(authenticationService.getUser("invalidToken"));
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));
121 public void isValid_nonExistingToken_returnsFalse() throws Exception {
122 assertFalse(authenticationService.isValid("invalidToken"));
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));
134 public void invalidate_invalidToken_returnsFalse() throws Exception {
135 assertFalse(authenticationService.invalidate("invalidToken"));