|
| 1 | +/* |
| 2 | + * Copyright 2015 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.example.appengine.users; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static org.mockito.Mockito.when; |
| 21 | + |
| 22 | +import com.google.appengine.tools.development.testing.LocalServiceTestHelper; |
| 23 | + |
| 24 | +import org.junit.After; |
| 25 | +import org.junit.Before; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | +import org.junit.runners.JUnit4; |
| 29 | +import org.mockito.Mock; |
| 30 | +import org.mockito.MockitoAnnotations; |
| 31 | + |
| 32 | +import java.io.PrintWriter; |
| 33 | +import java.io.StringWriter; |
| 34 | + |
| 35 | +import javax.management.remote.JMXPrincipal; |
| 36 | +import javax.servlet.http.HttpServletRequest; |
| 37 | +import javax.servlet.http.HttpServletResponse; |
| 38 | + |
| 39 | +/** |
| 40 | + * Unit tests for {@link UsersServlet}. |
| 41 | + */ |
| 42 | +@RunWith(JUnit4.class) |
| 43 | +public class UsersServletTest { |
| 44 | + private static final String FAKE_URL = "fakey.fake.fak"; |
| 45 | + private static final String FAKE_NAME = "Fake"; |
| 46 | + // Set up a helper so that the ApiProxy returns a valid environment for local testing. |
| 47 | + private final LocalServiceTestHelper helper = new LocalServiceTestHelper(); |
| 48 | + |
| 49 | + @Mock private HttpServletRequest mockRequestNotLoggedIn; |
| 50 | + @Mock private HttpServletRequest mockRequestLoggedIn; |
| 51 | + @Mock private HttpServletResponse mockResponse; |
| 52 | + private StringWriter responseWriter; |
| 53 | + private UsersServlet servletUnderTest; |
| 54 | + |
| 55 | + @Before |
| 56 | + public void setUp() throws Exception { |
| 57 | + MockitoAnnotations.initMocks(this); |
| 58 | + helper.setUp(); |
| 59 | + |
| 60 | + // Set up some fake HTTP requests |
| 61 | + // If the user isn't logged in, use this request |
| 62 | + when(mockRequestNotLoggedIn.getRequestURI()).thenReturn(FAKE_URL); |
| 63 | + when(mockRequestNotLoggedIn.getUserPrincipal()).thenReturn(null); |
| 64 | + |
| 65 | + // If the user is logged in, use this request |
| 66 | + when(mockRequestLoggedIn.getRequestURI()).thenReturn(FAKE_URL); |
| 67 | + // Most of the classes that implement Principal have been |
| 68 | + // deprecated. JMXPrincipal seems like a safe choice. |
| 69 | + when(mockRequestLoggedIn.getUserPrincipal()).thenReturn(new JMXPrincipal(FAKE_NAME)); |
| 70 | + |
| 71 | + // Set up a fake HTTP response. |
| 72 | + responseWriter = new StringWriter(); |
| 73 | + when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter)); |
| 74 | + |
| 75 | + servletUnderTest = new UsersServlet(); |
| 76 | + } |
| 77 | + |
| 78 | + @After public void tearDown() { |
| 79 | + helper.tearDown(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void doGet_userNotLoggedIn_writesResponse() throws Exception { |
| 84 | + servletUnderTest.doGet(mockRequestNotLoggedIn, mockResponse); |
| 85 | + |
| 86 | + // If a user isn't logged in, we expect a prompt |
| 87 | + // to login to be returned. |
| 88 | + assertThat(responseWriter.toString()) |
| 89 | + .named("UsersServlet response") |
| 90 | + .contains("<p>Please <a href="); |
| 91 | + assertThat(responseWriter.toString()) |
| 92 | + .named("UsersServlet response") |
| 93 | + .contains("sign in</a>.</p>"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void doGet_userLoggedIn_writesResponse() throws Exception { |
| 98 | + servletUnderTest.doGet(mockRequestLoggedIn, mockResponse); |
| 99 | + |
| 100 | + // If a user is logged in, we expect a prompt |
| 101 | + // to logout to be returned. |
| 102 | + assertThat(responseWriter.toString()) |
| 103 | + .named("UsersServlet response") |
| 104 | + .contains("<p>Hello, " + FAKE_NAME + "!"); |
| 105 | + assertThat(responseWriter.toString()) |
| 106 | + .named("UsersServlet response") |
| 107 | + .contains("sign out"); |
| 108 | + } |
| 109 | +} |
0 commit comments