test_google.py
8.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa. If not, see <http://www.gnu.org/licenses/>.
from default import Test
from pybossa.view.google import manage_user, manage_user_login
from mock import patch
from factories import UserFactory
class TestGoogle(Test):
def test_manage_user_new_user(self):
"""Test GOOGLE manage_user with a new user"""
user_data = dict(id='1', name='google', email='g@g.com')
token = 't'
user = manage_user(token, user_data)
assert user.email_addr == user_data['email'], user
assert user.name == user_data['name'], user
assert user.fullname == user_data['name'], user
assert user.google_user_id == user_data['id'], user
assert user.info['google_token'] == dict(oauth_token=token), user
def test_manage_user_twitter_registered_user(self):
"""Test GOOGLE manage_user with an existing user registered with Google"""
user_data = dict(id='1', name='google', email='g@g.com')
token = 't'
manage_user(token, user_data)
new_token = "new_t"
user = manage_user(new_token, user_data)
assert user.email_addr == user_data['email'], user
assert user.name == user_data['name'], user
assert user.fullname == user_data['name'], user
assert user.google_user_id == user_data['id'], user
assert user.info['google_token'] == dict(oauth_token=new_token), user
def test_manage_user_with_existing_non_twitter_account_user(self):
"""Test GOOGLE manage_user user with a username that already exists
and registered without Google"""
user_data = dict(id='10', name=self.name, email=self.email_addr)
token = 'tA'
user = manage_user(token, user_data)
err_msg = "User should be the same"
assert user.google_user_id == '10', err_msg
assert user.info['google_token'] == dict(oauth_token=token), user
@patch('pybossa.view.google.newsletter', autospec=True)
def test_manage_user_with_newsletter(self, newsletter):
"""Test GOOGLE manage_user with newsletter works."""
# First with a new user
newsletter.app = True
user_data = dict(id='1', name='google',
email='g@g.com')
token = 't'
user = manage_user(token, user_data)
assert user.email_addr == user_data['email'], user
assert user.name == user_data['name'], user
assert user.fullname == user_data['name'], user
assert user.google_user_id == user_data['id'], user
newsletter.subscribe_user.assert_called_once_with(user)
@patch('pybossa.view.google.newsletter', autospec=True)
def test_manage_user_with_newsletter_only_once(self, newsletter):
"""Test GOOGLE manage_user with newsletter only once works."""
# Second with the same user
token = 't'
user = UserFactory.create(fullname='john', name='john',
google_user_id='1')
user_data = dict(id=str(user.id), name=user.name, email=user.email_addr)
r_user = manage_user(token, user_data)
assert r_user.email_addr == user_data['email'], user
assert r_user.name == user_data['name'], user
assert r_user.fullname == user_data['name'], user
assert r_user.google_user_id == user_data['id'], user
assert newsletter.subscribe_user.called is False, newsletter.subscribe_user.called
@patch('pybossa.view.google.newsletter', autospec=True)
@patch('pybossa.view.google.login_user', return_value=True)
@patch('pybossa.view.google.flash', return_value=True)
@patch('pybossa.view.google.url_for', return_value=True)
@patch('pybossa.view.google.redirect', return_value=True)
def test_manage_user_with_oauth_newsletter(self, redirect,
url_for, flash,
login_user,
newsletter):
"""Test GOOGLE manage_user_login shows newsletter works."""
user = UserFactory.create(fullname='john', name='john',
google_user_id='1')
user_data = dict(id=str(user.id), name=user.name, email=user.email_addr)
next_url = '/'
manage_user_login(user, user_data, next_url=next_url)
login_user.assert_called_with(user, remember=True)
assert user.newsletter_prompted is False
url_for.assert_called_with('account.newsletter_subscribe',
next=next_url)
@patch('pybossa.view.google.newsletter', autospec=True)
@patch('pybossa.view.google.login_user', return_value=True)
@patch('pybossa.view.google.flash', return_value=True)
@patch('pybossa.view.google.url_for', return_value=True)
@patch('pybossa.view.google.redirect', return_value=True)
def test_manage_user_is_not_asked_twice(self, redirect,
url_for, flash,
login_user,
newsletter):
"""Test GOOGLE manage_user_login shows newsletter only once works."""
user = UserFactory.create(fullname='john', name='john',
google_user_id='1',
newsletter_prompted=True)
next_url = '/'
user_data = dict(id=str(user.id), name=user.name, email=user.email_addr)
manage_user_login(user, user_data, next_url=next_url)
login_user.assert_called_with(user, remember=True)
assert user.newsletter_prompted is True
assert url_for.called is False
@patch('pybossa.view.google.newsletter', autospec=True)
@patch('pybossa.view.google.login_user', return_value=True)
@patch('pybossa.view.google.flash', return_value=True)
@patch('pybossa.view.google.url_for', return_value=True)
@patch('pybossa.view.google.redirect', return_value=True)
def test_manage_login_without_user(self, redirect,
url_for, flash,
login_user,
newsletter):
"""Test GOOGLE manage_user_login without user works."""
user = UserFactory.create(fullname='john', name='john')
user_data = dict(id=str(user.id), name=user.name, email=user.email_addr)
next_url = '/'
manage_user_login(None, user_data, next_url=next_url)
assert login_user.called is False
url_for.assert_called_with('account.forgot_password')
@patch('pybossa.view.google.newsletter', autospec=True)
@patch('pybossa.view.google.login_user', return_value=True)
@patch('pybossa.view.google.flash', return_value=True)
@patch('pybossa.view.google.url_for', return_value=True)
@patch('pybossa.view.google.redirect', return_value=True)
def test_manage_login_without_user_facebook(self, redirect,
url_for, flash,
login_user,
newsletter):
"""Test GOOGLE manage_user_login without user facebook works."""
user = UserFactory.create(fullname='john', name='john',
info={'facebook_token': 't'})
user_data = dict(id=str(user.id), name=user.name, email=user.email_addr)
next_url = '/'
manage_user_login(None, user_data, next_url=next_url)
assert login_user.called is False
url_for.assert_called_with('account.signin')