/
test_page_settings_set_encoding.py
125 lines (106 loc) · 4.64 KB
/
test_page_settings_set_encoding.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
# -*- 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/>.
"""
Created on Jan 1, 2016
@author: Patrik Dufresne <patrik@ikus-soft.com>
"""
import rdiffweb.test
from rdiffweb.core.store import DEFAULT_REPO_ENCODING, USER_ROLE
class SetEncodingTest(rdiffweb.test.WebCase):
login = True
def _settings(self, user, repo):
self.getPage("/settings/" + user + "/" + repo + "/")
def _set_encoding(self, user, repo, encoding):
self.getPage("/settings/" + user + "/" + repo + "/", method="POST", body={'new_encoding': encoding})
def test_check_default_encoding(self):
# Default encoding for broker-repo is the default system encoding.
self._settings('admin', 'broker-repo')
self.assertInBody("Character encoding")
self.assertInBody('selected value="%s"' % DEFAULT_REPO_ENCODING)
def test_api_set_encoding(self):
"""
Check if /api/set-encoding/ is still working.
"""
self.getPage("/api/set-encoding/admin/testcases/", method="POST", body={'new_encoding': 'cp1252'})
self.assertStatus(200)
# Check results
user = self.app.store.get_user(self.USERNAME)
repo = user.get_repo(self.REPO)
self.assertEqual('cp1252', repo.encoding)
def test_set_encoding(self):
"""
Check to update the encoding with cp1252.
"""
self._set_encoding('admin', 'testcases', 'cp1252')
self.assertStatus(200)
self.assertInBody("Updated")
self.assertEqual('cp1252', self.app.store.get_user(self.USERNAME).get_repo(self.REPO).encoding)
# Get back encoding.
self._settings('admin', 'testcases')
self.assertInBody('selected value="cp1252"')
def test_set_encoding_capital_case(self):
"""
Check to update the encoding with US-ASCII.
"""
self._set_encoding('admin', 'testcases', 'US-ASCII')
self.assertStatus(200)
self.assertInBody("Updated")
self.assertEqual('ascii', self.app.store.get_user(self.USERNAME).get_repo(self.REPO).encoding)
# Get back encoding.
self._settings('admin', 'testcases')
self.assertInBody('selected value="ascii"')
def test_set_encoding_invalid(self):
"""
Check to update the encoding with invalid value.
"""
self._set_encoding('admin', 'testcases', 'invalid')
self.assertStatus(400)
self.assertInBody("invalid encoding value")
def test_set_encoding_windows_1252(self):
"""
Check to update the encoding with windows 1252.
"""
# Update encoding
self._set_encoding('admin', 'testcases', 'windows_1252')
self.assertStatus(200)
self.assertInBody("Updated")
# Get back encoding.
self._settings('admin', 'testcases')
self.assertInBody('selected value="cp1252"')
self.assertEqual('cp1252', self.app.store.get_user(self.USERNAME).get_repo(self.REPO).encoding)
def test_as_another_user(self):
# Create another user with admin right
user_obj = self.app.store.add_user('anotheruser', 'password')
user_obj.user_root = self.testcases
self._set_encoding('anotheruser', 'testcases', 'cp1252')
self.assertStatus('200 OK')
self.assertEqual('cp1252', user_obj.get_repo('testcases').encoding)
# Remove admin right
admin = self.app.store.get_user('admin')
admin.role = USER_ROLE
# Browse admin's repos
self._set_encoding('anotheruser', 'testcases', 'utf-8')
self.assertStatus('403 Forbidden')
def test_set_encoding_method_get(self):
# When trying to update encoding with method GET
self.getPage("/settings/admin/testcases/?new_encoding=cp1252")
# Then page return without error
self.assertStatus(200)
# Then database is not updated
user = self.app.store.get_user(self.USERNAME)
repo = user.get_repo(self.REPO)
self.assertEqual('utf-8', repo.encoding)