Skip to content
This repository was archived by the owner on Jan 26, 2023. It is now read-only.

Commit 034b410

Browse files
mark live API tests as optional, add validation for known option fields, replace own mocks with "responses" lib
1 parent 9310843 commit 034b410

File tree

8 files changed

+261
-148
lines changed

8 files changed

+261
-148
lines changed

conftest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pytest
2+
3+
# see https://docs.pytest.org/en/latest/example/simple.html#control-skipping-of-tests-according-to-command-line-option
4+
def pytest_addoption(parser):
5+
parser.addoption('--liveapi', action='store_true',
6+
default=False, help='run some tests againts live API')

gitcoin/client.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import requests
44

5+
import gitcoin.validation
56

67
class Config:
78
"""Define Base Class for API Endpoint Config."""
@@ -30,12 +31,12 @@ def __init__(self):
3031
"""Init params container for 'bounties' filters etc."""
3132
super().__init__()
3233
self.params = {
33-
'experience_level': (True, str),
34-
'project_length': (True, str),
35-
'bounty_type': (True, str),
34+
'experience_level': (True, gitcoin.validation.experience_level),
35+
'project_length': (True, gitcoin.validation.project_length),
36+
'bounty_type': (True, gitcoin.validation.bounty_type),
3637
'bounty_owner_address': (True, str),
3738
'bounty_owner_github_username': (True, str),
38-
'idx_status': (True, str),
39+
'idx_status': (True, gitcoin.validation.idx_status),
3940
'network': (True, str),
4041
'standard_bounties_id': (True, int),
4142
'pk__gt': (False, int),
@@ -45,7 +46,7 @@ def __init__(self):
4546
'fulfiller_github_username': (False, str),
4647
'interested_github_username': (False, str),
4748
'raw_data': (True, str),
48-
'order_by': (False, str),
49+
'order_by': (False, gitcoin.validation.order_by),
4950
'limit': (False, int),
5051
'offset': (False, int)
5152
}
@@ -119,7 +120,7 @@ def all(self):
119120

120121
def get(self, primary_key):
121122
"""Retrieve one resource by primary key."""
122-
return self._request_get('/'.join((self.url, str(primary_key))))
123+
return self._request_get(''.join((self.url, str(primary_key))))
123124

124125
def _request_get(self, url=None):
125126
"""Fire the actual HTTP GET request as configured."""
@@ -143,7 +144,7 @@ def __init__(self):
143144
self.set_class('endpoint', Endpoint)
144145
self.set_class('bounties_list_config', BountyConfig)
145146
self.urls = {}
146-
self.set_url('bounties', 'https://gitcoin.co/api/v0.1/bounties')
147+
self.set_url('bounties', 'https://gitcoin.co/api/v0.1/bounties/')
147148

148149
def set_class(self, cls_id, cls):
149150
"""Inject class dependency, overriding the default class."""

gitcoin/validation.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
# Valid parameter values as seen at
3+
# https://github.com/gitcoinco/web/blob/84babc30611c281c817582b4d677dda6366def83/app/dashboard/models.py#L119-L168
4+
options = {
5+
'experience_level': ['Beginner', 'Advanced', 'Intermediate', 'Unknown'],
6+
'project_length': ['Hours', 'Days', 'Weeks', 'Months', 'Unknown'],
7+
'bounty_type': ['Bug', 'Security', 'Feature', 'Unknown'],
8+
'idx_status': ['cancelled', 'done', 'expired', 'open', 'started', 'submitted', 'unknown'],
9+
'order_by': [
10+
'web3_type', 'title', 'web3_created', 'value_in_token', 'token_name',
11+
'token_address', 'bounty_type', 'project_length', 'experience_level',
12+
'github_url', 'github_comments', 'bounty_owner_address',
13+
'bounty_owner_email', 'bounty_owner_github_username',
14+
'bounty_owner_name', 'is_open', 'expires_date', 'raw_data', 'metadata',
15+
'current_bounty', '_val_usd_db', 'contract_address', 'network',
16+
'idx_experience_level', 'idx_project_length', 'idx_status',
17+
'issue_description', 'standard_bounties_id', 'num_fulfillments',
18+
'balance', 'accepted', 'interested', 'interested_comment',
19+
'submissions_comment', 'override_status', 'last_comment_date',
20+
'fulfillment_accepted_on', 'fulfillment_submitted_on',
21+
'fulfillment_started_on', 'canceled_on', 'snooze_warnings_for_days',
22+
'token_value_time_peg', 'token_value_in_usdt', 'value_in_usdt_now',
23+
'value_in_usdt', 'value_in_eth', 'value_true', 'privacy_preferences'
24+
]
25+
}
26+
27+
def _validate_options(field_name, value):
28+
if value in options[field_name]:
29+
return value
30+
msg = 'Unknown value "{val}" for field "{name}".'
31+
raise ValueError(msg.format(val=value, name=field_name))
32+
33+
def experience_level(value):
34+
return _validate_options('experience_level', value)
35+
36+
def project_length(value):
37+
return _validate_options('project_length', value)
38+
39+
def bounty_type(value):
40+
return _validate_options('bounty_type', value)
41+
42+
def idx_status(value):
43+
return _validate_options('idx_status', value)
44+
45+
def order_by(direction):
46+
if direction in options['order_by']:
47+
return direction
48+
if direction[0:1] == '-' and direction[1:] in options['order_by']:
49+
return direction
50+
msg = 'Unknown direction "{dir}" to order by.'
51+
raise ValueError(msg.format(dir=direction))

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ apipkg==1.4
22
attrs==18.1.0
33
certifi==2018.4.16
44
chardet==3.0.4
5+
cookies==2.2.1
56
coverage==4.5.1
67
execnet==1.5.0
78
flake8==3.5.0
@@ -19,6 +20,7 @@ pytest-cov==2.5.1
1920
pytest-isort==0.2.0
2021
pytest-runner==4.2
2122
requests==2.18.4
23+
responses==0.9.0
2224
six==1.11.0
2325
urllib3==1.22
2426
yapf==0.22.0

tests/mocks/__init__.py

Whitespace-only changes.

tests/mocks/requests.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)