Skip to content

Commit c6749dd

Browse files
committed
Merge pull request #164 from Annie29/master
Moved Users samples to Github
2 parents 1b1d17a + 46e05f1 commit c6749dd

File tree

7 files changed

+331
-0
lines changed

7 files changed

+331
-0
lines changed

appengine/users/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Users Authentication sample for Google App Engine
2+
3+
This sample demonstrates how to use the [Users API][appid] on [Google App
4+
Engine][ae-docs].
5+
6+
[appid]: https://cloud.google.com/appengine/docs/java/users/
7+
[ae-docs]: https://cloud.google.com/appengine/docs/java/
8+
9+
## Running locally
10+
This example uses the
11+
[Maven gcloud plugin](https://cloud.google.com/appengine/docs/java/managed-vms/maven).
12+
To run this sample locally:
13+
14+
$ mvn appengine:devserver
15+
16+
## Deploying
17+
In the following command, replace YOUR-PROJECT-ID with your
18+
[Google Cloud Project ID](https://developers.google.com/console/help/new/#projectnumber).
19+
20+
$ mvn appengine:update -Dappengine.appId=YOUR-PROJECT-ID -Dappengine.version=SOME-VERSION
21+
22+
## Setup
23+
To save your project settings so that you don't need to enter the
24+
parameters, you can:
25+
26+
1. Update the <application> tag in src/main/webapp/WEB-INF/appengine-web.xml
27+
with your project name.
28+
29+
2. Update the <version> tag in src/main/webapp/WEB-INF/appengine-web.xml
30+
with a valid version number.
31+
32+
33+
You will now be able to run
34+
35+
$ mvn appengine:update
36+
37+
without the need for any additional parameters.

appengine/users/pom.xml

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2015 Google Inc. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
18+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
19+
<modelVersion>4.0.0</modelVersion>
20+
<packaging>war</packaging>
21+
<version>1.0-SNAPSHOT</version>
22+
<groupId>com.example.appengine</groupId>
23+
<artifactId>appengine-users</artifactId>
24+
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). -->
25+
<parent>
26+
<groupId>com.google.cloud</groupId>
27+
<artifactId>doc-samples</artifactId>
28+
<version>1.0.0</version>
29+
<relativePath>../..</relativePath>
30+
</parent>
31+
<dependencies>
32+
<dependency>
33+
<groupId>com.google.appengine</groupId>
34+
<artifactId>appengine-maven-plugin</artifactId>
35+
<version>${appengine.sdk.version}</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.google.guava</groupId>
39+
<artifactId>guava</artifactId>
40+
<version>19.0</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>javax.servlet</groupId>
44+
<artifactId>servlet-api</artifactId>
45+
<version>2.5</version>
46+
<type>jar</type>
47+
<scope>provided</scope>
48+
</dependency>
49+
<dependency>
50+
<groupId>org.json</groupId>
51+
<artifactId>json</artifactId>
52+
<version>20151123</version>
53+
</dependency>
54+
<!-- Test Dependencies -->
55+
<dependency>
56+
<groupId>junit</groupId>
57+
<artifactId>junit</artifactId>
58+
<version>4.10</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.mockito</groupId>
63+
<artifactId>mockito-all</artifactId>
64+
<version>1.10.19</version>
65+
<scope>test</scope>
66+
</dependency>
67+
<dependency>
68+
<groupId>com.google.appengine</groupId>
69+
<artifactId>appengine-testing</artifactId>
70+
<version>${appengine.sdk.version}</version>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>com.google.appengine</groupId>
75+
<artifactId>appengine-api-stubs</artifactId>
76+
<version>${appengine.sdk.version}</version>
77+
<scope>test</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>com.google.appengine</groupId>
81+
<artifactId>appengine-tools-sdk</artifactId>
82+
<version>${appengine.sdk.version}</version>
83+
<scope>test</scope>
84+
</dependency>
85+
<dependency>
86+
<groupId>com.google.truth</groupId>
87+
<artifactId>truth</artifactId>
88+
<version>0.28</version>
89+
<scope>test</scope>
90+
</dependency>
91+
92+
</dependencies>
93+
<build>
94+
<!-- for hot reload of the web application -->
95+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
96+
<plugins>
97+
<plugin>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<version>3.3</version>
100+
<artifactId>maven-compiler-plugin</artifactId>
101+
<configuration>
102+
<source>1.7</source>
103+
<target>1.7</target>
104+
</configuration>
105+
</plugin>
106+
<plugin>
107+
<groupId>com.google.appengine</groupId>
108+
<artifactId>appengine-maven-plugin</artifactId>
109+
<version>${appengine.sdk.version}</version>
110+
</plugin>
111+
</plugins>
112+
</build>
113+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* Copyright 2016 Google Inc. All Rights Reserved.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
// [START users_API_example]
17+
package com.example.appengine.users;
18+
19+
import com.google.appengine.api.users.UserService;
20+
import com.google.appengine.api.users.UserServiceFactory;
21+
22+
import java.io.IOException;
23+
24+
import javax.servlet.http.HttpServlet;
25+
import javax.servlet.http.HttpServletRequest;
26+
import javax.servlet.http.HttpServletResponse;
27+
28+
public class UsersServlet extends HttpServlet {
29+
@Override
30+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
31+
throws IOException {
32+
UserService userService = UserServiceFactory.getUserService();
33+
34+
String thisUrl = req.getRequestURI();
35+
36+
resp.setContentType("text/html");
37+
if (req.getUserPrincipal() != null) {
38+
resp.getWriter().println("<p>Hello, "
39+
+ req.getUserPrincipal().getName()
40+
+ "! You can <a href=\""
41+
+ userService.createLogoutURL(thisUrl)
42+
+ "\">sign out</a>.</p>");
43+
} else {
44+
resp.getWriter().println("<p>Please <a href=\""
45+
+ userService.createLoginURL(thisUrl)
46+
+ "\">sign in</a>.</p>");
47+
}
48+
}
49+
}
50+
// [END users_API_example]
51+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
3+
<application>YOUR-PROJECT-ID</application>
4+
<version>YOUR-VERSION-ID</version>
5+
<threadsafe>true</threadsafe>
6+
</appengine-web-app>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
3+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
5+
version="2.5">
6+
<servlet>
7+
<servlet-name>users</servlet-name>
8+
<servlet-class>com.example.appengine.users.UsersServlet</servlet-class>
9+
</servlet>
10+
<servlet-mapping>
11+
<servlet-name>users</servlet-name>
12+
<url-pattern>/</url-pattern>
13+
</servlet-mapping>
14+
</web-app>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<module>appengine/static-files</module>
5454
<module>appengine/twilio</module>
5555
<module>appengine/urlfetch</module>
56+
<module>appengine/users</module>
5657
<module>bigquery</module>
5758
<module>compute/cmdline</module>
5859
<module>datastore</module>

0 commit comments

Comments
 (0)