|
| 1 | +"tests for event fabric API" |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import json |
| 5 | +import unittest |
| 6 | + |
| 7 | +TEST_DIR = os.path.dirname(__file__) |
| 8 | +BASE_DIR = os.path.join(TEST_DIR, '..') |
| 9 | +sys.path.append(BASE_DIR) |
| 10 | + |
| 11 | +import eventfabric as ef |
| 12 | + |
| 13 | +class FakeResponse(object): |
| 14 | + "fake response object" |
| 15 | + def __init__(self, status_code=200, cookies=None): |
| 16 | + self.status_code = status_code |
| 17 | + self.cookies = cookies |
| 18 | + |
| 19 | +def fake_post(storage, return_value): |
| 20 | + "build a fake post function" |
| 21 | + def fun(*args, **kwargs): |
| 22 | + "receives and stores all args, returns return_value" |
| 23 | + storage.append((args, kwargs)) |
| 24 | + return return_value |
| 25 | + |
| 26 | + return fun |
| 27 | + |
| 28 | + |
| 29 | +class TestEventFabric(unittest.TestCase): |
| 30 | + "tests event fabric api" |
| 31 | + |
| 32 | + def test_client_creation(self): |
| 33 | + "test that the client is created and parameters are set correctly" |
| 34 | + client = ef.Client("username", "password", |
| 35 | + "http://localhost:8080/ef/api") |
| 36 | + self.assertEqual(client.username, "username") |
| 37 | + self.assertEqual(client.password, "password") |
| 38 | + self.assertEqual(client.root_url, "http://localhost:8080/ef/api/") |
| 39 | + self.assertEqual(client.cookies, None) |
| 40 | + self.assertEqual(client.credentials["username"], "username") |
| 41 | + self.assertEqual(client.credentials["password"], "password") |
| 42 | + |
| 43 | + def test_endpoint(self): |
| 44 | + "tests that endpoints are created correctly" |
| 45 | + client = ef.Client("username", "password", |
| 46 | + "http://localhost:8080/ef/api") |
| 47 | + self.assertEqual(client.endpoint("session"), |
| 48 | + "http://localhost:8080/ef/api/session") |
| 49 | + |
| 50 | + def test_login(self): |
| 51 | + client = ef.Client("username", "password", |
| 52 | + "http://localhost:8080/ef/api") |
| 53 | + storage = [] |
| 54 | + requester = fake_post(storage, FakeResponse(200, "cookies!")) |
| 55 | + status, response = client.login(requester) |
| 56 | + args, kwargs = storage.pop() |
| 57 | + endpoint = args[0] |
| 58 | + data_arg = kwargs["data"] |
| 59 | + headers = kwargs["headers"] |
| 60 | + |
| 61 | + self.assertTrue(status) |
| 62 | + self.assertEqual(response.status_code, 200) |
| 63 | + self.assertEqual(response.cookies, "cookies!") |
| 64 | + self.assertEqual(response.cookies, client.cookies) |
| 65 | + self.assertEqual(data_arg, json.dumps(client.credentials)) |
| 66 | + self.assertEqual(headers["content-type"], "application/json") |
| 67 | + self.assertEqual(endpoint, "http://localhost:8080/ef/api/session") |
| 68 | + |
| 69 | + def test_send_event(self): |
| 70 | + client = ef.Client("username", "password", |
| 71 | + "http://localhost:8080/ef/api") |
| 72 | + storage = [] |
| 73 | + requester = fake_post(storage, FakeResponse(201)) |
| 74 | + data = {"name": "bob", "count": 10} |
| 75 | + channel = "my.channel" |
| 76 | + event = ef.Event(data, channel) |
| 77 | + status, response = client.send_event(event, requester) |
| 78 | + args, kwargs = storage.pop() |
| 79 | + endpoint = args[0] |
| 80 | + data_arg = kwargs["data"] |
| 81 | + headers = kwargs["headers"] |
| 82 | + |
| 83 | + self.assertTrue(status) |
| 84 | + self.assertEqual(response.status_code, 201) |
| 85 | + self.assertEqual(data_arg, json.dumps(event.json)) |
| 86 | + self.assertEqual(headers["content-type"], "application/json") |
| 87 | + self.assertEqual(endpoint, "http://localhost:8080/ef/api/event") |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + unittest.main() |
0 commit comments