/
test_page_prefs_tokens.py
109 lines (98 loc) · 4.35 KB
/
test_page_prefs_tokens.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
# -*- coding: utf-8 -*-
# rdiffweb, A web interface to rdiff-backup repositories
# Copyright (C) 2012-2021 rdiffweb contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import rdiffweb.test
from rdiffweb.core.model import Token, UserObject
class PagePrefTokensTest(rdiffweb.test.WebCase):
login = True
def test_get(self):
# When getting the page
self.getPage("/prefs/tokens")
# Then the page is return without error
self.assertStatus(200)
def test_add_access_token(self):
# Given an existing user
userobj = UserObject.get_user(self.USERNAME)
# When adding a new access token
self.getPage(
"/prefs/tokens",
method='POST',
body={'action': 'add_access_token', 'name': 'test-token-name', 'expiration_time': ''},
)
# Then page return without error
self.assertStatus(200)
# Then token name get displayed in the view
self.assertInBody('test-token-name')
# Then access token get created
self.assertEqual(1, Token.query.filter(Token.userid == userobj.userid, Token.name == 'test-token-name').count())
def test_add_access_token_with_expiration_time(self):
# Given an existing user
userobj = UserObject.get_user(self.USERNAME)
# When adding a new access token
self.getPage(
"/prefs/tokens",
method='POST',
body={'action': 'add_access_token', 'name': 'test-token-name', 'expiration_time': '1999-01-01'},
)
# Then page return without error
self.assertStatus(200)
# Then token name get displayed in the view
self.assertInBody('test-token-name')
# Then access token get created
self.assertEqual(1, Token.query.filter(Token.userid == userobj.userid, Token.name == 'test-token-name').count())
def test_add_access_token_without_name(self):
# Given an existing user
userobj = UserObject.get_user(self.USERNAME)
# When adding a new access token
self.getPage(
"/prefs/tokens",
method='POST',
body={'action': 'add_access_token', 'name': '', 'expiration_time': ''},
)
# Then page return without error
self.assertStatus(200)
# Then token name get displayed in the view
self.assertInBody('Token name: This field is required.')
# Then access token is not created
self.assertEqual(0, Token.query.filter(Token.userid == userobj.userid, Token.name == 'test-token-name').count())
def test_add_access_token_with_name_too_long(self):
# Given an existing user
# When adding a new access token with name too long.
self.getPage(
"/prefs/tokens",
method='POST',
body={'action': 'add_access_token', 'name': 'token' * 52, 'expiration_time': ''},
)
# Then page return with error message
self.assertStatus(200)
# Then token name get displayed in the view
self.assertInBody('Token name too long')
def test_delete_access_token(self):
# Given an existing user with access_token
userobj = UserObject.get_user(self.USERNAME)
userobj.add_access_token('test-token-name')
# When deleting access token
self.getPage(
"/prefs/tokens",
method='POST',
body={'action': 'delete_access_token', 'name': 'test-token-name'},
)
# Then page return without error
self.assertStatus(200)
# Then token name get displayed in the view
self.assertInBody('The access token has been successfully deleted.')
# Then access token is not created
self.assertEqual(0, Token.query.filter(Token.userid == userobj.userid, Token.name == 'test-token-name').count())