test_hateoas.py
10.5 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# -*- 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/>.
import json
from default import Test
from pybossa.hateoas import Hateoas
class TestHateoas(Test):
hateoas = Hateoas()
def setUp(self):
super(TestHateoas, self).setUp()
with self.flask_app.app_context():
self.create()
# Tests
def test_00_link_object(self):
"""Test HATEOAS object link is created"""
# For project
res = self.app.get("/api/project/1", follow_redirects=True)
output = json.loads(res.data)
err_msg = "There should be a Link with the object URI"
assert output['link'] is not None, err_msg
err_msg = "There should be a Links list with the category URI"
assert output['links'] is not None, err_msg
assert len(output['links']) == 1, err_msg
project_link = self.hateoas.link(rel='category', title='category',
href='http://localhost/api/category/1')
assert project_link == output['links'][0], err_msg
project_link = self.hateoas.link(rel='self', title='project',
href='http://localhost/api/project/1')
err_msg = "The object link is wrong: %s" % output['link']
assert project_link == output['link'], err_msg
# For task
res = self.app.get("/api/task/1", follow_redirects=True)
output = json.loads(res.data)
err_msg = "There should be a Link with the object URI"
assert output['link'] is not None, err_msg
task_link = self.hateoas.link(rel='self', title='task',
href='http://localhost/api/task/1')
err_msg = "The object link is wrong: %s" % output['link']
assert task_link == output['link'], err_msg
err_msg = "There should be one parent link: project"
assert output.get('links') is not None, err_msg
assert len(output.get('links')) == 1, err_msg
err_msg = "The parent link is wrong"
project_link = self.hateoas.link(rel='parent', title='project',
href='http://localhost/api/project/1')
assert output.get('links')[0] == project_link, err_msg
# For taskrun
res = self.app.get("/api/taskrun/1", follow_redirects=True)
output = json.loads(res.data)
err_msg = "There should be a Link with the object URI"
assert output['link'] is not None, err_msg
task_link = self.hateoas.link(rel='self', title='taskrun',
href='http://localhost/api/taskrun/1')
err_msg = "The object link is wrong: %s" % output['link']
assert task_link == output['link'], err_msg
err_msg = "There should be two parent links: project and task"
assert output.get('links') is not None, err_msg
assert len(output.get('links')) == 2, err_msg
err_msg = "The parent project link is wrong"
project_link = self.hateoas.link(rel='parent', title='project',
href='http://localhost/api/project/1')
assert output.get('links')[0] == project_link, err_msg
err_msg = "The parent task link is wrong"
project_link = self.hateoas.link(rel='parent', title='task',
href='http://localhost/api/task/1')
assert output.get('links')[1] == project_link, err_msg
res = self.app.post("/api/taskrun")
# For category
res = self.app.get("/api/category/1", follow_redirects=True)
output = json.loads(res.data)
err_msg = "There should be a Link with the object URI"
assert output['link'] is not None, err_msg
category_link = self.hateoas.link(rel='self', title='category',
href='http://localhost/api/category/1')
err_msg = "The object link is wrong: %s" % output['link']
assert category_link == output['link'], err_msg
err_msg = "There should be no other links"
assert output.get('links') is None, err_msg
err_msg = "The object links should are wrong"
# For user
# Pending define what user fields will be visible through the API
# Issue #626. For now let's suppose link and links are not visible
# res = self.app.get("/api/user/1?api_key=" + self.root_api_key, follow_redirects=True)
# output = json.loads(res.data)
# err_msg = "There should be a Link with the object URI"
# assert output['link'] is not None, err_msg
# user_link = self.hateoas.link(rel='self', title='user',
# href='http://localhost/api/user/1')
# err_msg = "The object link ir wrong: %s" % output['link']
# assert user_link == output['link'], err_msg
# # when the links specification of a user will be set, modify the following
# err_msg = "The list of links should be empty for now"
# assert output.get('links') == None, err_msg
def test_01_link_object(self):
"""Test HATEOAS object link is created"""
# For project
res = self.app.get("/api/project", follow_redirects=True)
output = json.loads(res.data)[0]
err_msg = "There should be a Link with the object URI"
assert output['link'] is not None, err_msg
project_link = self.hateoas.link(rel='self', title='project',
href='http://localhost/api/project/1')
err_msg = "The object link is wrong: %s" % output['link']
assert project_link == output['link'], err_msg
err_msg = "There should be a Links list with the category URI"
assert output['links'] is not None, err_msg
assert len(output['links']) == 1, err_msg
project_link = self.hateoas.link(rel='category', title='category',
href='http://localhost/api/category/1')
assert project_link == output['links'][0], err_msg
# For task
res = self.app.get("/api/task", follow_redirects=True)
output = json.loads(res.data)[0]
err_msg = "There should be a Link with the object URI"
assert output['link'] is not None, err_msg
task_link = self.hateoas.link(rel='self', title='task',
href='http://localhost/api/task/1')
err_msg = "The object link is wrong: %s" % output['link']
assert task_link == output['link'], err_msg
err_msg = "There should be one parent link: project"
assert output.get('links') is not None, err_msg
assert len(output.get('links')) == 1, err_msg
err_msg = "The parent link is wrong"
project_link = self.hateoas.link(rel='parent', title='project',
href='http://localhost/api/project/1')
assert output.get('links')[0] == project_link, project_link
# For taskrun
res = self.app.get("/api/taskrun", follow_redirects=True)
output = json.loads(res.data)[0]
err_msg = "There should be a Link with the object URI"
assert output['link'] is not None, err_msg
task_link = self.hateoas.link(rel='self', title='taskrun',
href='http://localhost/api/taskrun/1')
err_msg = "The object link is wrong: %s" % output['link']
assert task_link == output['link'], err_msg
err_msg = "There should be two parent links: project and task"
assert output.get('links') is not None, err_msg
assert len(output.get('links')) == 2, err_msg
err_msg = "The parent project link is wrong"
project_link = self.hateoas.link(rel='parent', title='project',
href='http://localhost/api/project/1')
assert output.get('links')[0] == project_link, err_msg
err_msg = "The parent task link is wrong"
project_link = self.hateoas.link(rel='parent', title='task',
href='http://localhost/api/task/1')
assert output.get('links')[1] == project_link, err_msg
# Check that hateoas removes all link and links from item
without_links = self.hateoas.remove_links(output)
err_msg = "There should not be any link or links keys"
assert without_links.get('link') is None, err_msg
assert without_links.get('links') is None, err_msg
# For category
res = self.app.get("/api/category", follow_redirects=True)
output = json.loads(res.data)[0]
err_msg = "There should be a Link with the object URI"
assert output['link'] is not None, err_msg
category_link = self.hateoas.link(rel='self', title='category',
href='http://localhost/api/category/1')
err_msg = "The object link is wrong: %s" % output['link']
assert category_link == output['link'], err_msg
err_msg = "There should be no other links"
assert output.get('links') is None, err_msg
err_msg = "The object links should are wrong"
# For user
# Pending define what user fields will be visible through the API
# Issue #626. For now let's suppose link and links are not visible
# res = self.app.get("/api/user?api_key=" + self.root_api_key, follow_redirects=True)
# output = json.loads(res.data)[0]
# err_msg = "There should be a Link with the object URI"
# assert output['link'] is not None, err_msg
# user_link = self.hateoas.link(rel='self', title='user',
# href='http://localhost/api/user/1')
# err_msg = "The object link ir wrong: %s" % output['link']
# assert user_link == output['link'], err_msg
# # when the links specification of a user will be set, modify the following
# err_msg = "The list of links should be empty for now"
# assert output.get('links') == None, err_msg