Skip to content

Commit 6ba300b

Browse files
committed
Moved Users samples to Github
1 parent 735a4cf commit 6ba300b

File tree

7 files changed

+342
-0
lines changed

7 files changed

+342
-0
lines changed

appengine/users/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 gcloud:run
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 gcloud:deploy -Dgcloud.gcloud_project=YOUR-PROJECT-ID
21+
22+
## Setup
23+
To save your project settings so that you don't need to enter the
24+
`-Dgcloud.gcloud_project=YOUR-CLOUD-PROJECT-ID` 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+
You will now be able to run
30+
31+
$ mvn gcloud:deploy
32+
33+
without the need for any additional parameters.

appengine/users/pom.xml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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-api-1.0-sdk</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>com.google.appengine</groupId>
99+
<artifactId>gcloud-maven-plugin</artifactId>
100+
<version>2.0.9.101.v20160316</version>
101+
</plugin>
102+
<plugin>
103+
<groupId>org.apache.maven.plugins</groupId>
104+
<version>3.3</version>
105+
<artifactId>maven-compiler-plugin</artifactId>
106+
<configuration>
107+
<source>1.7</source>
108+
<target>1.7</target>
109+
</configuration>
110+
</plugin>
111+
<plugin>
112+
<groupId>com.google.appengine</groupId>
113+
<artifactId>appengine-maven-plugin</artifactId>
114+
<version>${appengine.sdk.version}</version>
115+
</plugin>
116+
</plugins>
117+
</build>
118+
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
// [START users_API_example]
16+
package com.example.appengine.users;
17+
18+
import java.io.IOException;
19+
import javax.servlet.http.HttpServlet;
20+
import javax.servlet.http.HttpServletRequest;
21+
import javax.servlet.http.HttpServletResponse;
22+
import com.google.appengine.api.users.UserService;
23+
import com.google.appengine.api.users.UserServiceFactory;
24+
25+
public class UsersServlet extends HttpServlet {
26+
@Override
27+
public void doGet(HttpServletRequest req, HttpServletResponse resp)
28+
throws IOException {
29+
UserService userService = UserServiceFactory.getUserService();
30+
31+
String thisURL = req.getRequestURI();
32+
33+
resp.setContentType("text/html");
34+
if (req.getUserPrincipal() != null) {
35+
resp.getWriter().println("<p>Hello, " +
36+
req.getUserPrincipal().getName() +
37+
"! You can <a href=\"" +
38+
userService.createLogoutURL(thisURL) +
39+
"\">sign out</a>.</p>");
40+
} else {
41+
resp.getWriter().println("<p>Please <a href=\"" +
42+
userService.createLoginURL(thisURL) +
43+
"\">sign in</a>.</p>");
44+
}
45+
}
46+
}
47+
// [END users_API_example]
48+
Lines changed: 14 additions & 0 deletions
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.MyServlet</servlet-class>
9+
</servlet>
10+
<servlet-mapping>
11+
<servlet-name>users</servlet-name>
12+
<url-pattern>/</url-pattern>
13+
</servlet-mapping>
14+
</web-app>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
<vm>true</vm>
7+
</appengine-web-app>
Lines changed: 14 additions & 0 deletions
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.MyServlet</servlet-class>
9+
</servlet>
10+
<servlet-mapping>
11+
<servlet-name>users</servlet-name>
12+
<url-pattern>/</url-pattern>
13+
</servlet-mapping>
14+
</web-app>
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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.servlet.http.HttpServletRequest;
36+
import javax.servlet.http.HttpServletResponse;
37+
import javax.management.remote.JMXPrincipal;
38+
39+
/**
40+
* Unit tests for {@link UsersServlet}.
41+
*/
42+
@RunWith(JUnit4.class)
43+
public class UsersServletTest {
44+
private final String fakeURL = "fakey.fake.fak";
45+
private 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(fakeURL);
63+
when (mockRequestNotLoggedIn.getUserPrincipal()).thenReturn(null);
64+
65+
// If the user is logged in, use this request
66+
when (mockRequestLoggedIn.getRequestURI()).thenReturn(fakeURL);
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+
@Test
96+
public void doGet_userLoggedIn_writesResponse() throws Exception {
97+
servletUnderTest.doGet(mockRequestLoggedIn, mockResponse);
98+
99+
// If a user is logged in, we expect a prompt
100+
// to logout to be returned.
101+
assertThat(responseWriter.toString())
102+
.named("UsersServlet response")
103+
.contains("<p>Hello, " + FAKE_NAME + "!");
104+
assertThat(responseWriter.toString())
105+
.named("UsersServlet response")
106+
.contains("sign out");
107+
}
108+
}

0 commit comments

Comments
 (0)