test_cookies.py
3.37 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
# -*- 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 pybossa.cookies import CookieHandler
from mock import MagicMock
class TestCookieHandler(object):
def test_cookie_handler_adds_cookie_to_response(self):
"""Test that a CookieHandler object can successfully add a cookie to a
response object"""
mock_signer = MagicMock()
mock_signer.loads = lambda x: x
mock_signer.dumps = lambda x: x
mock_request = MagicMock(cookies={})
mock_response = MagicMock()
project = MagicMock(short_name='my_project')
user = 'someone'
cookie_handler = CookieHandler(request=mock_request, signer=mock_signer)
cookie_handler.add_cookie_to(mock_response, project, user)
mock_response.set_cookie.assert_called_with('my_projectpswd',
[user], max_age=1200)
def test_cookie_handler_updates_cookie(self):
"""Test that a CookieHandler updates the cookie with another user's info"""
mock_signer = MagicMock()
mock_signer.loads = lambda x: x
mock_signer.dumps = lambda x: x
mock_request = MagicMock(cookies={'my_projectpswd': ['first_user']})
mock_response = MagicMock()
project = MagicMock(short_name='my_project')
user = 'second_user'
cookie_handler = CookieHandler(request=mock_request, signer=mock_signer)
cookie_handler.add_cookie_to(mock_response, project, user)
mock_response.set_cookie.assert_called_with('my_projectpswd',
['first_user', user],
max_age=1200)
def test_cookie_handler_retrieves_cookie_if_exists(self):
"""Test that a CookieHandler retrieves a cookie form the request if that
exists"""
mock_signer = MagicMock()
mock_signer.loads = lambda x: x
mock_request = MagicMock(cookies={'my_projectpswd': ['a_user']})
project = MagicMock(short_name='my_project')
cookie_handler = CookieHandler(request=mock_request, signer=mock_signer)
cookie = cookie_handler.get_cookie_from(project)
assert cookie == ['a_user'], cookie
def test_cookie_handler_empty_list_if_no_cookie(self):
"""Test that a CookieHandler retrieves an empty list if the cookie for
a project does not exist"""
mock_signer = MagicMock()
mock_signer.loads = lambda x: x
mock_request = MagicMock(cookies={})
project = MagicMock(short_name='my_project')
cookie_handler = CookieHandler(request=mock_request, signer=mock_signer)
cookie = cookie_handler.get_cookie_from(project)
assert cookie == [], cookie