Skip to content

Commit

Permalink
Unit test the content of the login request
Browse files Browse the repository at this point in the history
Check the body of the request that we are sending, not just the result.
  • Loading branch information
jofegan committed Nov 15, 2020
1 parent c9d7216 commit 3fac3e2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
26 changes: 15 additions & 11 deletions tests/test_binding.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# (c) Copyright 2019 Hewlett Packard Enterprise Development LP
# (c) Copyright 2019-2020 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -23,19 +23,23 @@

class BindingTest(unittest.TestCase):
def setUp(self):
fake_url = 'http://api.example.com'
fake_key = 'opensesame'
fake_secret = 'thereisnospoon'
fake_token = 'fake-unit-test-token'
endpoint = 'https://api.example.com'
key = 'opensesame'
secret = 'thereisnospoon'
token = 'fake-unit-test-token'
hshake = 'grant_type=client_credentials&client_id=%s&client_secret=%s'
with requests_mock.Mocker() as m:
url = fake_url + '/auth/oauth/token'
expected = '{"access_token": "%s"}' % fake_token
m.post(url, text=expected)
url = endpoint + '/auth/oauth/token'
expected_send = hshake % (key, secret)
expected_receive = {'access_token': token}
adapter = m.post(url, json=expected_receive, complete_qs=True)
self.ormp = opsramp.binding.connect(
fake_url,
fake_key,
fake_secret
endpoint,
key,
secret
)
assert m.call_count == 1
assert adapter.last_request.text == expected_send
assert type(self.ormp) is opsramp.binding.Opsramp
assert 'Opsramp' in str(self.ormp)

Expand Down
13 changes: 8 additions & 5 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,24 @@ def test_create_update(self):
'country': 'US'
}
thisid = 555555
expected = {'id': thisid}
expected_send = good_definition
expected_receive = {'id': thisid}
url = self.clients.api.compute_url()
with requests_mock.Mocker() as m:
m.post(url, json=expected, complete_qs=True)
adapter = m.post(url, json=expected_receive, complete_qs=True)
actual = self.clients.create(definition=good_definition)
assert actual == expected
assert adapter.last_request.json() == expected_send
assert actual == expected_receive
# now try update
url = self.clients.api.compute_url(thisid)
with requests_mock.Mocker() as m:
m.post(url, json=expected, complete_qs=True)
adapter = m.post(url, json=expected_receive, complete_qs=True)
actual = self.clients.update(
uuid=thisid,
definition=good_definition
)
assert actual == expected
assert adapter.last_request.json() == expected_send
assert actual == expected_receive

def test_suspend(self):
thisid = 789012
Expand Down
13 changes: 7 additions & 6 deletions tests/test_tenant.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#
# (c) Copyright 2019 Hewlett Packard Enterprise Development LP
# (c) Copyright 2019-2020 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -23,16 +23,16 @@

class TenantTest(unittest.TestCase):
def setUp(self):
fake_url = 'http://api.example.com'
fake_url = 'https://api.example.com'
fake_token = 'unit-test-fake-token'
self.ormp = opsramp.binding.Opsramp(fake_url, fake_token)

self.fake_client_id = 'client_for_unit_test'
self.client = self.ormp.tenant(self.fake_client_id)
fake_client_id = 'client_for_unit_test'
self.client = self.ormp.tenant(fake_client_id)
assert self.client.is_client()

self.fake_msp_id = 'msp_for_unit_test'
self.msp = self.ormp.tenant(self.fake_msp_id)
fake_msp_id = 'msp_for_unit_test'
self.msp = self.ormp.tenant(fake_msp_id)
assert not self.msp.is_client()

def test_agent_script(self):
Expand All @@ -42,6 +42,7 @@ def test_agent_script(self):
m.get(url, text=expected)
actual = self.client.get_agent_script()
assert actual == expected
assert m.call_count == 1

def test_clients(self):
# A partner object can have clients contained within it.
Expand Down

0 comments on commit 3fac3e2

Please sign in to comment.