From 3fac3e26c534aa1a9c3b79de3835041996e2aa04 Mon Sep 17 00:00:00 2001 From: Josie Fegan Date: Sun, 15 Nov 2020 13:03:28 +0000 Subject: [PATCH] Unit test the content of the login request Check the body of the request that we are sending, not just the result. --- tests/test_binding.py | 26 +++++++++++++++----------- tests/test_client.py | 13 ++++++++----- tests/test_tenant.py | 13 +++++++------ 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/tests/test_binding.py b/tests/test_binding.py index e77211b..1961cbb 100644 --- a/tests/test_binding.py +++ b/tests/test_binding.py @@ -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. @@ -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) diff --git a/tests/test_client.py b/tests/test_client.py index 95791d2..3cc70cd 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -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 diff --git a/tests/test_tenant.py b/tests/test_tenant.py index d9c8f3c..ddfa35e 100644 --- a/tests/test_tenant.py +++ b/tests/test_tenant.py @@ -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. @@ -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): @@ -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.