-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from Annie29/master
Moved Users samples to Github
- Loading branch information
Showing
7 changed files
with
331 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <application> tag in src/main/webapp/WEB-INF/appengine-web.xml | ||
with your project name. | ||
|
||
2. Update the <version> 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
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. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<groupId>com.example.appengine</groupId> | ||
<artifactId>appengine-users</artifactId> | ||
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). --> | ||
<parent> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>doc-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../..</relativePath> | ||
</parent> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-maven-plugin</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>19.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>2.5</version> | ||
<type>jar</type> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.json</groupId> | ||
<artifactId>json</artifactId> | ||
<version>20151123</version> | ||
</dependency> | ||
<!-- Test Dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.10</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>1.10.19</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-testing</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-stubs</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-tools-sdk</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>0.28</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
<build> | ||
<!-- for hot reload of the web application --> | ||
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<version>3.3</version> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
</configuration> | ||
</plugin> | ||
<plugin> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-maven-plugin</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
51 changes: 51 additions & 0 deletions
51
appengine/users/src/main/java/com/example/appengine/users/UsersServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("<p>Hello, " | ||
+ req.getUserPrincipal().getName() | ||
+ "! You can <a href=\"" | ||
+ userService.createLogoutURL(thisUrl) | ||
+ "\">sign out</a>.</p>"); | ||
} else { | ||
resp.getWriter().println("<p>Please <a href=\"" | ||
+ userService.createLoginURL(thisUrl) | ||
+ "\">sign in</a>.</p>"); | ||
} | ||
} | ||
} | ||
// [END users_API_example] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> | ||
<application>YOUR-PROJECT-ID</application> | ||
<version>YOUR-VERSION-ID</version> | ||
<threadsafe>true</threadsafe> | ||
</appengine-web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" | ||
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" | ||
version="2.5"> | ||
<servlet> | ||
<servlet-name>users</servlet-name> | ||
<servlet-class>com.example.appengine.users.UsersServlet</servlet-class> | ||
</servlet> | ||
<servlet-mapping> | ||
<servlet-name>users</servlet-name> | ||
<url-pattern>/</url-pattern> | ||
</servlet-mapping> | ||
</web-app> |
109 changes: 109 additions & 0 deletions
109
appengine/users/src/test/java/com/example/appengine/users/UsersServletTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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("<p>Please <a href="); | ||
assertThat(responseWriter.toString()) | ||
.named("UsersServlet response") | ||
.contains("sign in</a>.</p>"); | ||
} | ||
|
||
@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("<p>Hello, " + FAKE_NAME + "!"); | ||
assertThat(responseWriter.toString()) | ||
.named("UsersServlet response") | ||
.contains("sign out"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters