Skip to content

Commit a232bef

Browse files
author
Atte Keltanen
committed
add some tests for network calls
1 parent bbf2bfa commit a232bef

File tree

5 files changed

+198
-5
lines changed

5 files changed

+198
-5
lines changed

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@
3030
'testdroid = testdroid:main',
3131
],
3232
},
33+
test_suite='testdroid.tests.test_all',
34+
tests_require=[
35+
'responses',
36+
],
3337
)

testdroid/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,14 @@ def download(self, path=None, filename=None, payload={}, callback=None):
234234
""" Upload file to API resource
235235
"""
236236
def upload(self, path=None, filename=None):
237-
url = "%s/api/v2/%s" % (self.cloud_url, path)
238-
files = {'file': open(filename, 'rb')}
239-
res = requests.post(url, files=files, headers=self._build_headers())
240-
if res.status_code not in list(range(200, 300)):
241-
raise RequestResponseError(res.text, res.status_code)
237+
# TOOD: where's the error handling?
238+
with open(filename, 'rb') as f:
239+
url = "%s/api/v2/%s" % (self.cloud_url, path)
240+
files = {'file': f}
241+
res = requests.post(url, files=files, headers=self._build_headers())
242+
if res.status_code not in list(range(200, 300)):
243+
raise RequestResponseError(res.text, res.status_code)
244+
return res
242245

243246
""" GET from API resource
244247
"""

testdroid/tests/__init__.py

Whitespace-only changes.

testdroid/tests/test_all.py

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from unittest import TestCase
4+
from unittest.mock import patch
5+
import io
6+
import sys
7+
import os
8+
9+
import testdroid
10+
import responses
11+
12+
URL_BASE = 'https://cloud.bitbar.com'
13+
URL_API = '{}/api/v2'.format(URL_BASE)
14+
URL_API_ME = '{}/me'.format(URL_API)
15+
URL_USERS = '{}/users'.format(URL_BASE)
16+
JSON = {'k': 'v'}
17+
PROJECT_ID = 2
18+
TEST_RUN_ID = 3
19+
DEVICE_RUN_ID = 4
20+
DEVICE_SESSION_ID = 5
21+
TAGS = 'tags'
22+
LIMIT = 0
23+
24+
t = testdroid.Testdroid()
25+
26+
27+
# check that API calls go where they should go
28+
class TestNetworking(TestCase):
29+
def setUp(self):
30+
# set up the token so gets, posts etc work
31+
url = '{}/oauth/token'.format(URL_BASE)
32+
json = {
33+
'access_token': 'token',
34+
'refresh_token': 'refresh',
35+
'expires_in': 65535
36+
}
37+
responses.add(responses.POST, url, json=json, status=200)
38+
39+
@responses.activate
40+
def test_get(self):
41+
path = 'get'
42+
url = '{}/{}'.format(URL_API, path)
43+
responses.add(responses.GET, url, json=JSON, status=200)
44+
response = t.get('get')
45+
self.assertEqual(response, JSON)
46+
47+
@responses.activate
48+
def test_get_post(self):
49+
path = 'post'
50+
url = '{}/{}'.format(URL_API, path)
51+
responses.add(responses.POST, url, json=JSON, status=200)
52+
response = t.post(path)
53+
self.assertEqual(response, JSON)
54+
55+
@responses.activate
56+
def test_delete(self):
57+
path = 'delete'
58+
url = '{}/{}'.format(URL_API, path)
59+
responses.add(responses.DELETE, url, json=JSON, status=200)
60+
response = t.delete(path)
61+
self.assertEqual(response.status_code, 200)
62+
63+
@responses.activate
64+
def test_upload(self):
65+
path = 'upload'
66+
file_path = '{}/testdroid/tests/upload.txt'.format(os.getcwd())
67+
url = '{}/{}'.format(URL_API, path)
68+
responses.add(responses.POST, url, json=JSON, status=200)
69+
response = t.upload(path, file_path)
70+
self.assertEqual(response.status_code, 200)
71+
72+
@responses.activate
73+
def test_get_me(self):
74+
url = URL_API_ME
75+
responses.add(responses.GET, url, json=JSON, status=200)
76+
self.assertEqual(t.get_me(), JSON)
77+
78+
@responses.activate
79+
def test_get_device_groups(self):
80+
url = '{}/device-groups'.format(URL_API_ME)
81+
responses.add(responses.GET, url, json=JSON, status=200)
82+
response = t.get_device_groups()
83+
self.assertEqual(response, JSON)
84+
85+
@responses.activate
86+
def test_get_frameworks(self):
87+
url = '{}/available-frameworks'.format(URL_API_ME)
88+
responses.add(responses.GET, url, json=JSON, status=200)
89+
response = t.get_frameworks()
90+
self.assertEqual(response, JSON)
91+
92+
@responses.activate
93+
def test_get_devices(self):
94+
url = '{}/devices'.format(URL_API)
95+
responses.add(responses.GET, url, json=JSON, status=200)
96+
response = t.get_devices()
97+
self.assertEqual(response, JSON)
98+
99+
@responses.activate
100+
def test_get_projects(self):
101+
url = '{}/projects'.format(URL_API_ME)
102+
responses.add(responses.GET, url, json=JSON, status=200)
103+
response = t.get_projects()
104+
self.assertEqual(response, JSON)
105+
106+
@responses.activate
107+
def test_get_project(self):
108+
PROJECT_ID = 2
109+
url = '{}/projects/{}'.format(URL_API_ME, PROJECT_ID)
110+
responses.add(responses.GET, url, json=JSON, status=200)
111+
response = t.get_project(PROJECT_ID)
112+
self.assertEqual(response, JSON)
113+
114+
@responses.activate
115+
def test_get_project_parameters(self):
116+
PROJECT_ID = 2
117+
url = '{}/projects/{}/config/parameters'.format(URL_API_ME, PROJECT_ID)
118+
responses.add(responses.GET, url, json=JSON, status=200)
119+
response = t.get_project_parameters(PROJECT_ID)
120+
self.assertEqual(response, JSON)
121+
122+
@responses.activate
123+
def test_get_project_config(self):
124+
PROJECT_ID = 2
125+
url = '{}/projects/{}/config'.format(URL_API_ME, PROJECT_ID)
126+
responses.add(responses.GET, url, json=JSON, status=200)
127+
response = t.get_project_config(PROJECT_ID)
128+
self.assertEqual(response, JSON)
129+
130+
@responses.activate
131+
def test_get_project_test_runs(self):
132+
url = '{}/projects/{}/runs'.format(URL_API_ME, PROJECT_ID)
133+
responses.add(responses.GET, url, json=JSON, status=200)
134+
response = t.get_project_test_runs(PROJECT_ID)
135+
self.assertEqual(response, JSON)
136+
137+
@responses.activate
138+
def test_get_test_run(self):
139+
url = '{}/projects/{}/runs/{}'.format(URL_API_ME, PROJECT_ID,
140+
TEST_RUN_ID)
141+
responses.add(responses.GET, url, json=JSON, status=200)
142+
response = t.get_test_run(PROJECT_ID, TEST_RUN_ID)
143+
self.assertEqual(response, JSON)
144+
145+
@responses.activate
146+
def test_get_device_runs(self):
147+
url = '{}/projects/{}/runs/{}/device-runs'.format(
148+
URL_API_ME, PROJECT_ID, TEST_RUN_ID)
149+
responses.add(responses.GET, url, json=JSON, status=200)
150+
response = t.get_device_runs(PROJECT_ID, TEST_RUN_ID)
151+
self.assertEqual(response, JSON)
152+
153+
@responses.activate
154+
def test_get_device_run_screenshots_list(self):
155+
url = '{}/projects/{}/runs/{}/device-runs/{}/screenshots'.format(
156+
URL_API_ME, PROJECT_ID, TEST_RUN_ID, DEVICE_RUN_ID)
157+
responses.add(responses.GET, url, json=JSON, status=200)
158+
response = t.get_device_run_screenshots_list(PROJECT_ID, TEST_RUN_ID,
159+
DEVICE_RUN_ID)
160+
self.assertEqual(response, JSON)
161+
162+
@responses.activate
163+
def test_get_device_run_files_without_tags(self):
164+
url = '{}/projects/{}/runs/{}/device-sessions/{}/output-file-set/files'.format(
165+
URL_API_ME, PROJECT_ID, TEST_RUN_ID, DEVICE_SESSION_ID)
166+
responses.add(responses.GET, url, json=JSON, status=200)
167+
response = t.get_device_run_files(PROJECT_ID, TEST_RUN_ID,
168+
DEVICE_SESSION_ID)
169+
self.assertEqual(response, JSON)
170+
171+
@responses.activate
172+
def test_get_device_run_files_with_tags(self):
173+
url = '{}/projects/{}/runs/{}/device-sessions/{}/output-file-set/files?tag[]?={}'.format(
174+
URL_API_ME, PROJECT_ID, TEST_RUN_ID, DEVICE_SESSION_ID, TAGS)
175+
responses.add(responses.GET, url, json=JSON, status=200)
176+
response = t.get_device_run_files(PROJECT_ID, TEST_RUN_ID,
177+
DEVICE_SESSION_ID, TAGS)
178+
self.assertEqual(response, JSON)
179+
180+
@responses.activate
181+
def test_get_input_files(self):
182+
url = '{}/files?limit={}&filter=s_direction_eq_INPUT'.format(
183+
URL_API_ME, LIMIT)
184+
responses.add(responses.GET, url, json=JSON, status=200)
185+
self.assertEqual(t.get_input_files(LIMIT), JSON)

testdroid/tests/upload.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
upload this

0 commit comments

Comments
 (0)