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