diff --git a/appengine/users/README.md b/appengine/users/README.md new file mode 100644 index 00000000000..4d6d60a6a02 --- /dev/null +++ b/appengine/users/README.md @@ -0,0 +1,37 @@ +# Users Authentication sample for Google App Engine + +This sample demonstrates how to use the [Users API][appid] on [Google App +Engine][ae-docs]. + +[appid]: https://cloud.google.com/appengine/docs/java/users/ +[ae-docs]: https://cloud.google.com/appengine/docs/java/ + +## Running locally +This example uses the +[Maven gcloud plugin](https://cloud.google.com/appengine/docs/java/managed-vms/maven). +To run this sample locally: + + $ mvn appengine:devserver + +## Deploying +In the following command, replace YOUR-PROJECT-ID with your +[Google Cloud Project ID](https://developers.google.com/console/help/new/#projectnumber). + + $ mvn appengine:update -Dappengine.appId=YOUR-PROJECT-ID -Dappengine.version=SOME-VERSION + +## Setup +To save your project settings so that you don't need to enter the + parameters, you can: + +1. Update the tag in src/main/webapp/WEB-INF/appengine-web.xml + with your project name. + +2. Update the tag in src/main/webapp/WEB-INF/appengine-web.xml + with a valid version number. + + +You will now be able to run + + $ mvn appengine:update + +without the need for any additional parameters. diff --git a/appengine/users/pom.xml b/appengine/users/pom.xml new file mode 100644 index 00000000000..415bdcd793b --- /dev/null +++ b/appengine/users/pom.xml @@ -0,0 +1,113 @@ + + + + 4.0.0 + war + 1.0-SNAPSHOT + com.example.appengine + appengine-users + + + com.google.cloud + doc-samples + 1.0.0 + ../.. + + + + com.google.appengine + appengine-maven-plugin + ${appengine.sdk.version} + + + com.google.guava + guava + 19.0 + + + javax.servlet + servlet-api + 2.5 + jar + provided + + + org.json + json + 20151123 + + + + junit + junit + 4.10 + test + + + org.mockito + mockito-all + 1.10.19 + test + + + com.google.appengine + appengine-testing + ${appengine.sdk.version} + test + + + com.google.appengine + appengine-api-stubs + ${appengine.sdk.version} + test + + + com.google.appengine + appengine-tools-sdk + ${appengine.sdk.version} + test + + + com.google.truth + truth + 0.28 + test + + + + + + ${project.build.directory}/${project.build.finalName}/WEB-INF/classes + + + org.apache.maven.plugins + 3.3 + maven-compiler-plugin + + 1.7 + 1.7 + + + + com.google.appengine + appengine-maven-plugin + ${appengine.sdk.version} + + + + diff --git a/appengine/users/src/main/java/com/example/appengine/users/UsersServlet.java b/appengine/users/src/main/java/com/example/appengine/users/UsersServlet.java new file mode 100644 index 00000000000..cd976a1820c --- /dev/null +++ b/appengine/users/src/main/java/com/example/appengine/users/UsersServlet.java @@ -0,0 +1,51 @@ +/* Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// [START users_API_example] +package com.example.appengine.users; + +import com.google.appengine.api.users.UserService; +import com.google.appengine.api.users.UserServiceFactory; + +import java.io.IOException; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class UsersServlet extends HttpServlet { + @Override + public void doGet(HttpServletRequest req, HttpServletResponse resp) + throws IOException { + UserService userService = UserServiceFactory.getUserService(); + + String thisUrl = req.getRequestURI(); + + resp.setContentType("text/html"); + if (req.getUserPrincipal() != null) { + resp.getWriter().println("

Hello, " + + req.getUserPrincipal().getName() + + "! You can sign out.

"); + } else { + resp.getWriter().println("

Please sign in.

"); + } + } +} +// [END users_API_example] + diff --git a/appengine/users/src/main/webapp/WEB-INF/appengine-web.xml b/appengine/users/src/main/webapp/WEB-INF/appengine-web.xml new file mode 100644 index 00000000000..c9e245399bf --- /dev/null +++ b/appengine/users/src/main/webapp/WEB-INF/appengine-web.xml @@ -0,0 +1,6 @@ + + + YOUR-PROJECT-ID + YOUR-VERSION-ID + true + diff --git a/appengine/users/src/main/webapp/WEB-INF/web.xml b/appengine/users/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000000..4b2b77234dc --- /dev/null +++ b/appengine/users/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,14 @@ + + + + users + com.example.appengine.users.UsersServlet + + + users + / + + diff --git a/appengine/users/src/test/java/com/example/appengine/users/UsersServletTest.java b/appengine/users/src/test/java/com/example/appengine/users/UsersServletTest.java new file mode 100644 index 00000000000..b710ae737d0 --- /dev/null +++ b/appengine/users/src/test/java/com/example/appengine/users/UsersServletTest.java @@ -0,0 +1,109 @@ +/* + * Copyright 2015 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.appengine.users; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.when; + +import com.google.appengine.tools.development.testing.LocalServiceTestHelper; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.io.PrintWriter; +import java.io.StringWriter; + +import javax.management.remote.JMXPrincipal; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * Unit tests for {@link UsersServlet}. + */ +@RunWith(JUnit4.class) +public class UsersServletTest { + private static final String FAKE_URL = "fakey.fake.fak"; + private static final String FAKE_NAME = "Fake"; + // Set up a helper so that the ApiProxy returns a valid environment for local testing. + private final LocalServiceTestHelper helper = new LocalServiceTestHelper(); + + @Mock private HttpServletRequest mockRequestNotLoggedIn; + @Mock private HttpServletRequest mockRequestLoggedIn; + @Mock private HttpServletResponse mockResponse; + private StringWriter responseWriter; + private UsersServlet servletUnderTest; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + helper.setUp(); + + // Set up some fake HTTP requests + // If the user isn't logged in, use this request + when(mockRequestNotLoggedIn.getRequestURI()).thenReturn(FAKE_URL); + when(mockRequestNotLoggedIn.getUserPrincipal()).thenReturn(null); + + // If the user is logged in, use this request + when(mockRequestLoggedIn.getRequestURI()).thenReturn(FAKE_URL); + // Most of the classes that implement Principal have been + // deprecated. JMXPrincipal seems like a safe choice. + when(mockRequestLoggedIn.getUserPrincipal()).thenReturn(new JMXPrincipal(FAKE_NAME)); + + // Set up a fake HTTP response. + responseWriter = new StringWriter(); + when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter)); + + servletUnderTest = new UsersServlet(); + } + + @After public void tearDown() { + helper.tearDown(); + } + + @Test + public void doGet_userNotLoggedIn_writesResponse() throws Exception { + servletUnderTest.doGet(mockRequestNotLoggedIn, mockResponse); + + // If a user isn't logged in, we expect a prompt + // to login to be returned. + assertThat(responseWriter.toString()) + .named("UsersServlet response") + .contains("

Please .

"); + } + + @Test + public void doGet_userLoggedIn_writesResponse() throws Exception { + servletUnderTest.doGet(mockRequestLoggedIn, mockResponse); + + // If a user is logged in, we expect a prompt + // to logout to be returned. + assertThat(responseWriter.toString()) + .named("UsersServlet response") + .contains("

Hello, " + FAKE_NAME + "!"); + assertThat(responseWriter.toString()) + .named("UsersServlet response") + .contains("sign out"); + } +} diff --git a/pom.xml b/pom.xml index d35d36bc8a0..cb178bd6ae5 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,7 @@ appengine/static-files appengine/twilio appengine/urlfetch + appengine/users bigquery datastore logging