Skip to content

Commit 6873cd7

Browse files
author
Pascal Boutin
authored
Merge pull request #46 from mbelang/random-ids
Generate random client and contact ids
2 parents a999d0c + dc1967e commit 6873cd7

File tree

4 files changed

+25
-24
lines changed

4 files changed

+25
-24
lines changed

fake_ubersmith/api/methods/client.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from fake_ubersmith.api.base import Base
1616
from fake_ubersmith.api.ubersmith import FakeUbersmithError
1717
from fake_ubersmith.api.utils.response import response
18+
from fake_ubersmith.api.utils.utils import a_random_id
1819

1920

2021
class Client(Base):
@@ -67,7 +68,7 @@ def client_add(self, form_data):
6768

6869
client_data = form_data.copy()
6970
client_data["clientid"] = client_id
70-
client_data["contact_id"] = str(0)
71+
client_data["contact_id"] = str(a_random_id())
7172

7273
if client_data.get("uber_login"):
7374
client_data["login"] = client_data.get("uber_login")
@@ -108,7 +109,7 @@ def client_get(self, form_data):
108109
)
109110

110111
def contact_add(self, form_data):
111-
contact_id = str(len(self.data_store.contacts) + 1)
112+
contact_id = str(a_random_id())
112113

113114
contact_data = form_data.copy()
114115
contact_data["contact_id"] = contact_id

fake_ubersmith/api/utils/utils.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from random import randint
2+
3+
4+
def a_random_id():
5+
return randint(100000, 999999)
+11-12
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Copyright 2017 Internap.
22
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
44
# you may not use this file except in compliance with the License.
55
# You may obtain a copy of the License at
66
#
77
# http://www.apache.org/licenses/LICENSE-2.0
88
#
99
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
@@ -25,27 +25,26 @@ def test_client_creation_works(self):
2525

2626
result = self.ub_client.client.contact_list(client_id=client_id)
2727

28-
self.assertEqual(result.get('1').get("client_id"), client_id)
29-
self.assertEqual(result.get('1').get("description"), 'Primary Contact')
28+
contacts = list(result.values())
29+
30+
self.assertEqual(contacts[0].get('client_id'), client_id)
31+
self.assertEqual(contacts[0].get('description'), 'Primary Contact')
3032

3133
def test_list_all_contacts_works(self):
3234
self.ub_client.client.contact_add(client_id='1234')
3335
self.ub_client.client.contact_add(client_id='1234')
3436
self.ub_client.client.contact_add(client_id='1235')
3537

3638
result = self.ub_client.client.contact_list(client_id='1234')
39+
contacts = list(result.values())
3740

38-
self.assertDictEqual(
39-
result,
40-
{
41-
'2': {'client_id': '1234', 'contact_id': '2'},
42-
'3': {'client_id': '1234', 'contact_id': '3'}
43-
}
44-
)
41+
self.assertEqual(len(contacts), 2)
42+
self.assertEqual(contacts[0].get('client_id'), '1234')
43+
self.assertEqual(contacts[1].get('client_id'), '1234')
4544

4645
def test_list_all_contacts_raises_if_client_id_not_found(self):
4746
with self.assertRaises(UbersmithException) as exc:
4847
self.ub_client.client.contact_list(client_id='1234')
4948

5049
self.assertEqual(exc.exception.code, 1)
51-
self.assertEqual(exc.exception.message, "Invalid client_id specified.")
50+
self.assertEqual(exc.exception.message, 'Invalid client_id specified.')

tests/unit/api/methods/test_client.py

+6-10
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_client_add_creates_a_client(self):
5959
}
6060
)
6161
self.assertEqual(self.data_store.clients[0]["login"], "john")
62-
self.assertEqual(self.data_store.contacts[0]["contact_id"], "1")
62+
self.assertIsInstance(self.data_store.contacts[0]["contact_id"], str)
6363
self.assertEqual(self.data_store.contacts[0]["client_id"], body.get("data"))
6464
self.assertEqual(self.data_store.contacts[0]["description"], "Primary Contact")
6565

@@ -144,16 +144,12 @@ def test_client_contact_add_creates_a_contact(self):
144144
}
145145
)
146146

147+
body = json.loads(resp.data.decode('utf-8'))
147148
self.assertEqual(resp.status_code, 200)
148-
self.assertEqual(
149-
json.loads(resp.data.decode('utf-8')),
150-
{
151-
"data": "1",
152-
"error_code": None,
153-
"error_message": "",
154-
"status": True
155-
}
156-
)
149+
self.assertIsNone(body.get("error_code"))
150+
self.assertTrue(body.get("status"))
151+
self.assertEqual(body.get("error_message"), "")
152+
self.assertIsInstance(body.get("data"), str)
157153

158154
def test_client_contact_get_returns_error_when_empty_payload_provided(self):
159155
with self.app.test_client() as c:

0 commit comments

Comments
 (0)