test_webhook.py
5.44 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
# -*- 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 helper import web
from default import with_context
from factories import ProjectFactory, TaskFactory, AnonymousTaskRunFactory
from pybossa.core import user_repo, webhook_repo
from pybossa.model import make_timestamp
from pybossa.model.webhook import Webhook
class TestWebhookView(web.Helper):
def payload(self, project, task):
return dict(fired_at=make_timestamp(),
project_short_name=project.short_name,
project_id= project.id,
event='task_completed',
task_id=task.id)
@with_context
def test_webhook_handler_anon(self):
"""Test WEBHOOK view works for anons."""
project = ProjectFactory.create()
url = "/project/%s/webhook" % project.short_name
res = self.app.get(url, follow_redirects=True)
assert "Sign in" in res.data, res.data
@with_context
def test_webhook_handler_auth(self):
"""Test WEBHOOK view works for authenticated not owner."""
# Admin
self.register()
self.signout()
# Owner
self.register()
owner = user_repo.get(1)
project = ProjectFactory.create(owner=owner)
self.signout()
# User
self.register(name="juan")
url = "/project/%s/webhook" % project.short_name
res = self.app.get(url)
assert res.status_code == 403, res.status_code
@with_context
def test_webhook_handler_owner_non_pro(self):
"""Test WEBHOOK view works for non pro owner."""
# Admin
self.register()
self.signout()
# User
self.register(name="Iser")
owner = user_repo.get_by(name="Iser")
project = ProjectFactory.create(owner=owner)
url = "/project/%s/webhook" % project.short_name
res = self.app.get(url)
assert res.status_code == 403, res.status_code
@with_context
def test_webhook_handler_owner_pro(self):
"""Test WEBHOOK view works for pro owner."""
# Admin/owner
self.register()
self.signout()
# User
self.register(name="Iser")
owner = user_repo.get_by(name="Iser")
owner.pro = True
user_repo.save(owner)
project = ProjectFactory.create(owner=owner)
url = "/project/%s/webhook" % project.short_name
res = self.app.get(url)
assert res.status_code == 200, res.status_code
assert "Created" in res.data
assert "Payload" in res.data
@with_context
def test_webhook_handler_admin(self):
"""Test WEBHOOK view works for admin."""
# Admin
self.register()
self.signout()
# User
self.register(name="user", password="user")
owner = user_repo.get(2)
self.signout()
# Access as admin
self.signin()
project = ProjectFactory.create(owner=owner)
url = "/project/%s/webhook" % project.short_name
res = self.app.get(url)
assert res.status_code == 200, res.status_code
assert "Created" in res.data
assert "Payload" in res.data
@with_context
def test_webhook_handler_post_oid(self):
"""Test WEBHOOK post oid works."""
self.register()
user = user_repo.get(1)
project = ProjectFactory.create(owner=user)
task = TaskFactory.create(project=project, n_answers=1)
AnonymousTaskRunFactory.create(project=project, task=task)
payload = self.payload(project, task)
webhook = Webhook(project_id=project.id, payload=payload,
response='OK', response_status_code=200)
webhook_repo.save(webhook)
webhook = webhook_repo.get(1)
url = "/project/%s/webhook/%s" % (project.short_name, webhook.id)
res = self.app.post(url)
tmp = json.loads(res.data)
assert res.status_code == 200, res.status_code
assert tmp['payload']['project_short_name'] == project.short_name
assert tmp['payload']['project_id'] == project.id
assert tmp['payload']['task_id'] == task.id
@with_context
def test_webhook_handler_post_oid_404(self):
"""Test WEBHOOK post oid 404 works."""
self.register()
user = user_repo.get(1)
project = ProjectFactory.create(owner=user)
task = TaskFactory.create(project=project, n_answers=1)
AnonymousTaskRunFactory.create(project=project, task=task)
payload = self.payload(project, task)
webhook = Webhook(project_id=project.id, payload=payload,
response='OK', response_status_code=200)
webhook_repo.save(webhook)
url = "/project/%s/webhook/%s" % (project.short_name, 9999)
res = self.app.post(url)
assert res.status_code == 404, res.status_code