Skip to content

Commit b2702c4

Browse files
author
Teun Zengerink
committed
Add tests for the added resources
Added a couple of tests to make sure the newly added resources are considered valid. While doing so did a minor refactor.
1 parent 86802ff commit b2702c4

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

tests.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import sys
23
import unittest
34
import usabilla as ub
@@ -6,10 +7,9 @@
67
from unittest import TestCase, main as unittest_main
78

89

9-
import logging
10-
1110
logging.basicConfig(level=logging.DEBUG)
1211

12+
1313
class TestCredentials(TestCase):
1414

1515
def setUp(self):
@@ -66,18 +66,15 @@ def test_client_constants(self):
6666
self.assertEqual(self.client.host_protocol, 'https://')
6767
self.assertEqual('',self.client.query_parameters)
6868

69-
7069
def test_sign_key(self):
7170
signed_key = self.client.sign(self.secret_key.encode('utf-8'), 'usbl1_request'.encode('utf-8'))
7271
self.assertEqual(signed_key, b"&-\x88\x80Z9\xe8Pnvx\xe4S\xeeZ\x9fG\xc5\xf7g\x11|\xc1\xaa~q(\xef\xaf\x95\xc0\xac")
7372

74-
7573
def test_get_signature_key(self):
7674
datestamp = '20150115'
7775
signing_key = self.client.get_signature_key(self.secret_key, datestamp)
7876
self.assertEqual(signing_key, b"\x15\x8d\xd7U\xceG\xdeH\x8aHwU\xf5qg\xae\xd4Z\x19`\xedM\x80\x87\x97V\xbf\xe9pw\xaa\xae")
7977

80-
8178
def test_query_parameters(self):
8279
params = {'limit': 1}
8380
self.client.set_query_parameters(params)
@@ -88,12 +85,32 @@ def test_query_parameters(self):
8885

8986
def test_check_resource_validity(self):
9087
with self.assertRaises(ub.GeneralError):
91-
self.client.check_resource_validity('nonexisting', 'nonexisting', 'nonexisting')
88+
self.client.check_resource_validity(
89+
'nonexisting',
90+
'nonexisting',
91+
'nonexisting')
9292
with self.assertRaises(ub.GeneralError):
93-
self.client.check_resource_validity('live', 'nonexisting', 'nonexisting')
93+
self.client.check_resource_validity(
94+
'live',
95+
'nonexisting',
96+
'nonexisting')
9497
with self.assertRaises(ub.GeneralError):
95-
self.client.check_resource_validity('live', 'websites', 'nonexisting')
96-
self.assertEqual(self.client.check_resource_validity('live', 'websites', 'button'), '/live/websites/button')
98+
self.client.check_resource_validity(
99+
'live',
100+
'websites',
101+
'nonexisting')
102+
self.assertEqual(
103+
self.client.check_resource_validity('live', 'websites', 'button'),
104+
'/live/websites/button')
105+
self.assertEqual(
106+
self.client.check_resource_validity('live', 'apps', 'campaign'),
107+
'/live/apps/campaign')
108+
self.assertEqual(
109+
self.client.check_resource_validity(
110+
'live',
111+
'apps',
112+
'campaign_result'),
113+
'/live/apps/campaign/:id/results')
97114

98115
def test_handle_id(self):
99116
url = '/live/websites/button/:id/feedback'

usabilla.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ def send_signed_request(self, scope):
239239
request_url = self.host + scope + '?' + canonical_querystring
240240
r = requests.get(self.host_protocol + request_url, headers=headers)
241241

242-
243242
if r.status_code != 200:
244243
return r
245244
else:
@@ -262,14 +261,15 @@ def check_resource_validity(self, scope, product, resource):
262261
"""
263262
if scope not in self.resources['scopes'].keys():
264263
raise GeneralError('invalid scope', 'Invalid scope name')
265-
if product not in self.resources['scopes'][scope]['products'].keys():
264+
found_scope = self.resources['scopes'][scope]
265+
if product not in found_scope['products'].keys():
266266
raise GeneralError('invalid product', 'Invalid product name')
267-
if resource not in self.resources['scopes'][scope]['products'][product]['resources'].keys():
267+
found_product = found_scope['products'][product]
268+
if resource not in found_product['resources'].keys():
268269
raise GeneralError('invalid resource', 'Invalid resource name')
270+
found_resource = found_product['resources'][resource]
269271

270-
url = '/' + scope + '/' + product + self.resources['scopes'][scope]['products'][product]['resources'][resource]
271-
272-
return url
272+
return '/%s/%s%s' % (scope, product, found_resource)
273273

274274
def handle_id(self, url, resource_id):
275275
"""Replaces the :id pattern in the url

0 commit comments

Comments
 (0)