1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.academics.service;
16
17 import org.apache.cxf.jaxrs.ext.MessageContext;
18 import org.apache.log4j.Logger;
19 import org.junit.AfterClass;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.kuali.mobility.academics.util.AcademicsConstants;
25 import org.kuali.mobility.security.authn.util.AuthenticationConstants;
26 import org.kuali.mobility.security.user.api.User;
27 import org.kuali.mobility.security.user.api.UserDao;
28 import org.kuali.mobility.security.user.entity.UserAttribute;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.beans.factory.annotation.Qualifier;
31 import org.springframework.mock.web.MockHttpServletRequest;
32 import org.springframework.test.context.ContextConfiguration;
33 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
34
35 import javax.persistence.EntityManager;
36 import javax.persistence.PersistenceContext;
37 import javax.servlet.http.HttpSession;
38 import java.util.List;
39
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertTrue;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44
45
46
47
48 @RunWith(SpringJUnit4ClassRunner.class)
49 @ContextConfiguration(value = "classpath:TestSpringBeans.xml")
50 public class AcademicsAuthServiceImplTest {
51 private static final Logger LOG = Logger.getLogger(AcademicsAuthServiceImplTest.class);
52
53 @PersistenceContext
54 private EntityManager entityManager;
55
56 @Autowired
57 @Qualifier("academicsAuthService")
58 private AcademicsAuthServiceImpl academicsAuthService;
59
60 @Autowired
61 @Qualifier("kmeUserDao")
62 private UserDao userDao;
63
64 private MessageContext messageContext;
65 private MockHttpServletRequest httpServletRequest;
66
67 @BeforeClass
68 public static void setUpClass() throws Exception {
69 }
70
71 @AfterClass
72 public static void tearDownClass() throws Exception {
73 }
74
75 @Before
76 public void preTest() {
77 setHttpServletRequest(new MockHttpServletRequest());
78 setMessageContext(mock(MessageContext.class));
79 getAcademicsAuthService().setMessageContext(this.getMessageContext());
80 when(getMessageContext().getHttpServletRequest()).thenReturn(this.getHttpServletRequest());
81 }
82
83 @Test
84 public void testGradeAlertOptIn() {
85 getHttpServletRequest().setRemoteUser("mojojojo");
86 HttpSession session = getHttpServletRequest().getSession();
87 session.setAttribute(AuthenticationConstants.DEVICE_ID,"deviceIdB");
88 boolean optIn = getAcademicsAuthService().updateGradeAlertOpt("ON");
89 assertTrue("Failed to opt in for grade alerts.",optIn);
90 User user = getUserDao().loadUserByLoginName("mojojojo");
91 assertFalse("Failed to find user in database.",user==null);
92 List<UserAttribute> attributes = user.getAttribute(AcademicsConstants.USER_ATTR_GRADEALERT);
93 assertFalse("Attribute not found.",attributes==null);
94 assertFalse("Attribute not found.",attributes.isEmpty());
95 assertTrue("More than one attribute for grade alert opt in found.",attributes.size()==1);
96 assertTrue("Failed to save opt in status.","on".equalsIgnoreCase(attributes.get(0).getAttributeValue()));
97 }
98
99 @Test
100 public void testGradeAlertOptOut() {
101 getHttpServletRequest().setRemoteUser("blossom");
102 HttpSession session = getHttpServletRequest().getSession();
103 session.setAttribute(AuthenticationConstants.DEVICE_ID,"AbCdEfGhIjKlMnOpQrStUvWxYz");
104 boolean optIn = getAcademicsAuthService().updateGradeAlertOpt("OFF");
105 assertTrue("Failed to opt in for grade alerts.",optIn);
106 User user = getUserDao().loadUserByLoginName("blossom");
107 assertFalse("Failed to find user in database.",user==null);
108 List<UserAttribute> attributes = user.getAttribute(AcademicsConstants.USER_ATTR_GRADEALERT);
109 assertFalse("Attribute not found.",attributes==null);
110 assertFalse("Attribute not found.",attributes.isEmpty());
111 LOG.debug("====== Found "+attributes.size()+" attributes for grade alerts.");
112 assertTrue("More than one attribute for grade alert opt in found.",attributes.size()==1);
113 for( UserAttribute attribute : attributes ) {
114 LOG.debug("====== Attribute Name ["+attribute.getAttributeName()+"] Value ["+attribute.getAttributeValue()+"]");
115 }
116 assertTrue("Failed to save opt in status.","off".equalsIgnoreCase(attributes.get(0).getAttributeValue()));
117 }
118
119 @Test
120 public void testGradeAlertWithNullRemoteUser() {
121 boolean optIn = getAcademicsAuthService().updateGradeAlertOpt("ON");
122 assertFalse("Opt in succeeded and shouldn't have.",optIn);
123 }
124
125 @Test
126 public void testGradeAlertWithEmptyRemoteUser() {
127 getHttpServletRequest().setRemoteUser("");
128 boolean optIn = getAcademicsAuthService().updateGradeAlertOpt("ON");
129 assertFalse("Opt in succeeded and shouldn't have.",optIn);
130 }
131
132 @Test
133 public void testGradeAlertWithBadUser() {
134 getHttpServletRequest().setRemoteUser("utonium");
135 boolean optIn = getAcademicsAuthService().updateGradeAlertOpt("ON");
136 assertFalse("Opt in succeeded and shouldn't have.",optIn);
137 }
138
139 @Test
140 public void testGradeAlertWithNullRequest() {
141 when(getMessageContext().getHttpServletRequest()).thenReturn(null);
142 boolean optIn = getAcademicsAuthService().updateGradeAlertOpt("ON");
143 assertFalse("Opt in succeeded and shouldn't have.",optIn);
144 }
145
146 @Test
147 public void testGradeAlertWithNullSession() {
148 getHttpServletRequest().setRemoteUser("blossom");
149 getHttpServletRequest().setSession(null);
150 boolean optIn = getAcademicsAuthService().updateGradeAlertOpt("ON");
151 assertTrue("Opt in succeeded.",optIn);
152 }
153
154 public EntityManager getEntityManager() {
155 return entityManager;
156 }
157
158 public void setEntityManager(EntityManager entityManager) {
159 this.entityManager = entityManager;
160 }
161
162 public AcademicsAuthServiceImpl getAcademicsAuthService() {
163 return academicsAuthService;
164 }
165
166 public void setAcademicsAuthService(AcademicsAuthServiceImpl academicsAuthService) {
167 this.academicsAuthService = academicsAuthService;
168 }
169
170 public MessageContext getMessageContext() {
171 return messageContext;
172 }
173
174 public void setMessageContext(MessageContext messageContext) {
175 this.messageContext = messageContext;
176 }
177
178 public MockHttpServletRequest getHttpServletRequest() {
179 return httpServletRequest;
180 }
181
182 public void setHttpServletRequest(MockHttpServletRequest httpServletRequest) {
183 this.httpServletRequest = httpServletRequest;
184 }
185
186 public UserDao getUserDao() {
187 return userDao;
188 }
189
190 public void setUserDao(UserDao userDao) {
191 this.userDao = userDao;
192 }
193 }