Beispiel #1
0
 def test_when_ServerBridge_initializes_with_key_it_asks_for_a_token(self):
     with patch('ubidots.apiclient.requests') as mock_request:
         ServerBridge.initialize = self.original_initialize
         apikey = "anyapikey"
         sb = ServerBridge(apikey)
         mock_request.post.assert_called_once_with(
             "%s%s" % (sb.base_url, "auth/token"),
             headers={'content-type': 'application/json', 'X-UBIDOTS-APIKEY': 'anyapikey'}
         )
Beispiel #2
0
class TestServerBridge(unittest.TestCase):

    def setUp(self):
        self.original_initialize = ServerBridge.initialize
        ServerBridge.initialize = MagicMock()
        apikey = "anyapikey"
        self.serverbridge = ServerBridge(apikey)
        self.serverbridge._token_header = {'X-AUTH-TOKEN': 'the token'}

    def tearDown(self):
        ServerBridge.initialize = self.original_initialize

    def test_when_ServerBridge_initializes_with_key_it_asks_for_a_token(self):
        with patch('ubidots.apiclient.requests') as mock_request:
            ServerBridge.initialize = self.original_initialize
            apikey = "anyapikey"
            sb = ServerBridge(apikey)
            mock_request.post.assert_called_once_with(
                "%s%s" % (sb.base_url, "auth/token"),
                headers={'content-type': 'application/json', 'X-UBIDOTS-APIKEY': 'anyapikey'}
            )

    def test_when_ServerBridge_initializes_with_token_it_set_it_correctly(self):
            sb = ServerBridge(token="anytoken")
            self.assertEqual(sb._token_header, {'X-AUTH-TOKEN': 'anytoken'})

    def test_get_includes_specific_headers(self):
        with patch('ubidots.apiclient.requests') as mock_request:
            self.serverbridge.get("any/path")

            mock_request.get.assert_called_once_with(
                "%s%s" % (self.serverbridge.base_url, "any/path"),
                headers={'content-type': 'application/json', 'X-AUTH-TOKEN': 'the token'}
            )

    def test_post_includes_specific_headers_and_data(self):
        with patch('ubidots.apiclient.requests') as mock_request:
            data = {"dataone": 1, "datatwo": 2}
            self.serverbridge.post("any/path", data)

            mock_request.post.assert_called_once_with(
                "%s%s" % (self.serverbridge.base_url, "any/path"),
                headers={'content-type': 'application/json', 'X-AUTH-TOKEN': 'the token'},
                data=json.dumps(data)
            )

    def test_delete_includes_specific_headers(self):
        with patch('ubidots.apiclient.requests') as mock_request:
            self.serverbridge.delete("any/path")

            mock_request.delete.assert_called_once_with(
                "%s%s" % (self.serverbridge.base_url, "any/path"),
                headers={'content-type': 'application/json', 'X-AUTH-TOKEN': 'the token'},
            )
Beispiel #3
0
 def test_when_ServerBridge_initializes_with_token_it_set_it_correctly(self):
         sb = ServerBridge(token="anytoken")
         self.assertEqual(sb._token_header, {'X-AUTH-TOKEN': 'anytoken'})
Beispiel #4
0
 def setUp(self):
     self.original_initialize = ServerBridge.initialize
     ServerBridge.initialize = MagicMock()
     apikey = "anyapikey"
     self.serverbridge = ServerBridge(apikey)
     self.serverbridge._token_header = {'X-AUTH-TOKEN': 'the token'}