-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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 #265 from GoogleCloudPlatform/appengine-users
Moving app engine users example
- Loading branch information
Showing
5 changed files
with
127 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
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
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,7 @@ | ||
runtime: python27 | ||
threadsafe: yes | ||
api_version: 1 | ||
|
||
handlers: | ||
- url: .* | ||
script: main.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,60 @@ | ||
# Copyright 2016 Google Inc. | ||
# | ||
# 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. | ||
|
||
""" | ||
Sample Google App Engine application that demonstrates using the Users API | ||
For more information about App Engine, see README.md under /appengine. | ||
""" | ||
|
||
# [START all] | ||
|
||
from google.appengine.api import users | ||
import webapp2 | ||
|
||
|
||
class MainPage(webapp2.RequestHandler): | ||
def get(self): | ||
user = users.get_current_user() | ||
if user: | ||
nickname = user.nickname() | ||
logout_url = users.create_logout_url('/') | ||
greeting = 'Welcome, {}! (<a href="{}">sign out</a>)'.format( | ||
nickname, logout_url) | ||
else: | ||
login_url = users.create_login_url('/') | ||
greeting = '<a href="{}">Sign in</a>'.format(login_url) | ||
|
||
self.response.write( | ||
'<html><body>{}</body></html>'.format(greeting)) | ||
|
||
|
||
class AdminPage(webapp2.RequestHandler): | ||
def get(self): | ||
user = users.get_current_user() | ||
if user: | ||
if users.is_current_user_admin(): | ||
self.response.write('You are an administrator.') | ||
else: | ||
self.response.write('You are not an administrator.') | ||
else: | ||
self.response.write('You are not logged in.') | ||
|
||
|
||
app = webapp2.WSGIApplication([ | ||
('/', MainPage), | ||
('/admin', AdminPage) | ||
], debug=True) | ||
|
||
# [END all] |
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,43 @@ | ||
# 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. | ||
|
||
import main | ||
import webtest | ||
|
||
|
||
def test_index(testbed, login): | ||
app = webtest.TestApp(main.app) | ||
|
||
response = app.get('/') | ||
assert 'Login' in response.body | ||
|
||
login() | ||
response = app.get('/') | ||
assert 'Logout' in response.body | ||
assert 'user@example.com' in response.body | ||
|
||
|
||
def test_admin(testbed, login): | ||
app = webtest.TestApp(main.app) | ||
|
||
response = app.get('/admin') | ||
assert 'You are not logged in' in response.body | ||
|
||
login() | ||
response = app.get('/admin') | ||
assert 'You are not an administrator' in response.body | ||
|
||
login(is_admin=True) | ||
response = app.get('/admin') | ||
assert 'You are an administrator' in response.body |