/
test_webapp.py
171 lines (122 loc) · 5.27 KB
/
test_webapp.py
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
164
165
166
167
168
169
170
171
import os
os.environ['SECRET_KEY'] = '1234'
os.environ['CAMPUS_NET_APP_NAME'] = '123'
os.environ['CAMPUS_NET_APP_TOKEN'] = '1234'
os.environ['CONFIG_FILE'] = 'app.cfg.example'
from webapp import (app, SessionAuthentication, UserCVBuilder)
from pytest import yield_fixture
from mock import Mock, MagicMock
from base64 import b64encode as base64
import webapp
class FakeCampusNetPicture(object):
def iter_content(self, size):
return ['user', '_picture']
class FakeCampusNetClient(object):
def __init__(self):
self.success = True
self.auth_token = 'LSSJK-28SJS'
def is_authenticated(self):
return self.success
def authenticate(self, username, password):
pass
def authenticate_with_token(self, student_id, auth_token):
pass
def user(self):
return MagicMock(user_id=1234)
def user_picture(self, user_id):
return FakeCampusNetPicture()
class NullObject(object):
def __call__(self):
return NullObject()
def __getattr__(self, name):
return NullObject()
def __iter__(self):
return self
def __next__(self):
raise StopIteration
def next(self):
self.__next__()
@yield_fixture
def api():
app.debug = True
app.testing = True
yield app.test_client()
@yield_fixture
def fake_cn_client(monkeypatch):
cn_client = FakeCampusNetClient()
monkeypatch.setattr(webapp, 'campus_net_client', cn_client)
yield cn_client
def _auth_headers():
basic_auth_creds = base64("usrname:secret".encode('ascii'))
return {
'Authorization': 'Basic %s' % basic_auth_creds.decode('ascii'),
'Content-Type': 'application/json'
}
def assert_redirected_to(response, path):
assert response.status_code == 302
assert response.location == "http://localhost%s" % path
def test_root_simply_render(api):
respone = api.get('/')
assert respone.status_code == 200
def test_authenticated_users_are_redirected_to_cv_in_root(api, monkeypatch):
monkeypatch.setattr(SessionAuthentication, 'is_authenticated',
lambda self: True)
response = api.get('/', follow_redirects=False)
assert_redirected_to(response, '/cv')
def test_correct_authentications_responds_with_200(api, monkeypatch,
fake_cn_client):
response = api.get('/auth', headers=_auth_headers())
assert response.status_code == 200
def test_authentication_with_correct_credentials(api, monkeypatch):
campus_net_client_mock = Mock(auth_token='LSSJK-28SJS')
monkeypatch.setattr(webapp, 'campus_net_client', campus_net_client_mock)
api.get('/auth', headers=_auth_headers())
campus_net_client_mock.authenticate.assert_called_with('usrname',
'secret')
def test_correct_authentication_updates_session(api, monkeypatch,
fake_cn_client):
session_auth_mock = Mock(return_value=None)
monkeypatch.setattr(SessionAuthentication, 'authenticate',
session_auth_mock)
api.get('/auth', headers=_auth_headers())
session_auth_mock.assert_called_with('usrname', 'LSSJK-28SJS')
def test_bad_authentication_responds_with_401(api, monkeypatch,
fake_cn_client):
fake_cn_client.success = False
response = api.get('/auth', headers=_auth_headers())
assert response.status_code == 401
def test_log_out_invokes_session_log_out(api, monkeypatch):
session_auth_mock = Mock(return_value=None)
monkeypatch.setattr(SessionAuthentication, 'log_out',
session_auth_mock)
api.post('/log_out')
session_auth_mock.assert_called_with()
def test_log_out_redirects_to_root(api, monkeypatch):
monkeypatch.setattr(SessionAuthentication, 'log_out', lambda x: None)
response = api.post('/log_out')
assert_redirected_to(response, '/')
def test_cv_page_redirects_to_root_when_unauthenticated(api, monkeypatch,
fake_cn_client):
monkeypatch.setattr(SessionAuthentication, 'is_authenticated',
lambda self: False)
response = api.get('/cv')
assert_redirected_to(response, '/')
def test_cv_page_renders_the_cv_for_authenticated_users(api, monkeypatch,
fake_cn_client):
monkeypatch.setattr(SessionAuthentication, 'is_authenticated',
lambda self: True)
monkeypatch.setattr(UserCVBuilder, 'build', lambda self: NullObject())
response = api.get('/cv')
assert response.status_code == 200
def test_picture_responds_with_401_when_uanauthenticate(api, monkeypatch,
fake_cn_client):
fake_cn_client.success = False
response = api.get('/cv/picture')
assert response.status_code == 401
def test_picture_responds_with_image_when_authenticated(api, monkeypatch,
fake_cn_client):
monkeypatch.setattr(SessionAuthentication, 'is_authenticated',
lambda self: True)
response = api.get('/cv/picture')
assert response.status_code == 200
assert response.data == 'user_picture'.encode('ascii')