|
| 1 | +# Copyright 2015 Google Inc. |
| 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 | +import contextlib |
| 16 | + |
| 17 | +from conftest import flaky_filter |
| 18 | +from flaky import flaky |
| 19 | +from oauth2client.client import OAuth2Credentials |
| 20 | +import pytest |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def client_with_credentials(app): |
| 25 | + """This fixture provides a Flask app test client that has a session |
| 26 | + pre-configured with use credentials.""" |
| 27 | + credentials = OAuth2Credentials( |
| 28 | + 'access_token', |
| 29 | + 'client_id', |
| 30 | + 'client_secret', |
| 31 | + 'refresh_token', |
| 32 | + '3600', |
| 33 | + None, |
| 34 | + 'Test', |
| 35 | + id_token={'sub': '123', 'email': 'user@example.com'}, |
| 36 | + scopes=('email', 'profile')) |
| 37 | + |
| 38 | + @contextlib.contextmanager |
| 39 | + def inner(): |
| 40 | + with app.test_client() as client: |
| 41 | + with client.session_transaction() as session: |
| 42 | + session['profile'] = {'id': 'abc', 'displayName': 'Test User'} |
| 43 | + session['google_oauth2_credentials'] = credentials.to_json() |
| 44 | + yield client |
| 45 | + |
| 46 | + return inner |
| 47 | + |
| 48 | + |
| 49 | +# Mark all test cases in this class as flaky, so that if errors occur they |
| 50 | +# can be retried. This is useful when databases are temporarily unavailable. |
| 51 | +@flaky(rerun_filter=flaky_filter) |
| 52 | +# Tell pytest to use both the app and model fixtures for all test cases. |
| 53 | +# This ensures that configuration is properly applied and that all database |
| 54 | +# resources created during tests are cleaned up. These fixtures are defined |
| 55 | +# in conftest.py |
| 56 | +@pytest.mark.usefixtures('app', 'model') |
| 57 | +class TestAuth(object): |
| 58 | + |
| 59 | + def test_not_logged_in(self, app): |
| 60 | + with app.test_client() as c: |
| 61 | + rv = c.get('/books/') |
| 62 | + |
| 63 | + assert rv.status == '200 OK' |
| 64 | + body = rv.data.decode('utf-8') |
| 65 | + assert 'Login' in body |
| 66 | + |
| 67 | + def test_logged_in(self, client_with_credentials): |
| 68 | + with client_with_credentials() as c: |
| 69 | + rv = c.get('/books/') |
| 70 | + |
| 71 | + assert rv.status == '200 OK' |
| 72 | + body = rv.data.decode('utf-8') |
| 73 | + assert 'Test User' in body |
| 74 | + |
| 75 | + def test_add_anonymous(self, app): |
| 76 | + data = { |
| 77 | + 'title': 'Test Book', |
| 78 | + } |
| 79 | + |
| 80 | + with app.test_client() as c: |
| 81 | + rv = c.post('/books/add', data=data, follow_redirects=True) |
| 82 | + |
| 83 | + assert rv.status == '200 OK' |
| 84 | + body = rv.data.decode('utf-8') |
| 85 | + assert 'Test Book' in body |
| 86 | + assert 'Added by Anonymous' in body |
| 87 | + |
| 88 | + def test_add_logged_in(self, client_with_credentials): |
| 89 | + data = { |
| 90 | + 'title': 'Test Book', |
| 91 | + } |
| 92 | + |
| 93 | + with client_with_credentials() as c: |
| 94 | + rv = c.post('/books/add', data=data, follow_redirects=True) |
| 95 | + |
| 96 | + assert rv.status == '200 OK' |
| 97 | + body = rv.data.decode('utf-8') |
| 98 | + assert 'Test Book' in body |
| 99 | + assert 'Added by Test User' in body |
| 100 | + |
| 101 | + def test_mine(self, model, client_with_credentials): |
| 102 | + # Create two books, one created by the logged in user and one |
| 103 | + # created by another user. |
| 104 | + model.create({ |
| 105 | + 'title': 'Book 1', |
| 106 | + 'createdById': 'abc' |
| 107 | + }) |
| 108 | + |
| 109 | + model.create({ |
| 110 | + 'title': 'Book 2', |
| 111 | + 'createdById': 'def' |
| 112 | + }) |
| 113 | + |
| 114 | + # Check the "My Books" page and make sure only one of the books |
| 115 | + # appears. |
| 116 | + with client_with_credentials() as c: |
| 117 | + rv = c.get('/books/mine') |
| 118 | + |
| 119 | + assert rv.status == '200 OK' |
| 120 | + body = rv.data.decode('utf-8') |
| 121 | + assert 'Book 1' in body |
| 122 | + assert 'Book 2' not in body |
0 commit comments