Coverage for tests/test_microservice_websocket/test_routes/test_route_organizations.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.0.0, created at 2022-12-20 14:31 +0000

1import pytest 

2from fastapi.testclient import TestClient 

3 

4from microservice_websocket.app.services import database as db 

5 

6 

7class TestPostOrganizations: 

8 endpoint = "/api/organization" 

9 name = "orgName" 

10 

11 # Creating Organization 

12 @pytest.mark.asyncio 

13 async def test_create(self, app_client: TestClient, auth_header): 

14 response = app_client.post( 

15 self.endpoint, 

16 json={"name": self.name}, 

17 headers=auth_header, 

18 ) 

19 

20 assert ( 

21 response.status_code == 200 

22 ), "Invalid response code when posting valid data" 

23 

24 assert ( 

25 len(await db.Organization.find_all().to_list()) == 1 

26 ), "Create organization doesn't persist in the database" 

27 

28 assert ( 

29 org := await db.Organization.find_one() 

30 ) and org.organizationName == self.name, "Invalid organization name" 

31 

32 # Creating Organization with already used name 

33 @pytest.mark.asyncio 

34 async def test_create_invalid_name(self, app_client: TestClient, auth_header): 

35 o = db.Organization(organizationName=self.name) 

36 await o.save() 

37 # Done setup 

38 

39 response = app_client.post( 

40 self.endpoint, json={"name": self.name}, headers=auth_header 

41 ) 

42 

43 assert ( 

44 response.status_code == 400 

45 ), "Invalid response code when posting data with name already in use" 

46 

47 # Creating organization with invalid payload 

48 def test_create_invalid_payload(self, app_client: TestClient, auth_header): 

49 response = app_client.post(self.endpoint, json={}, headers=auth_header) 

50 

51 assert ( 

52 response.status_code == 422 

53 ), "Invalid response code when posting an invalid payload when creating an organization" 

54 

55 

56class TestGetOrganizations: 

57 endpoint = "/api/organizations" 

58 name = "orgName" 

59 

60 @pytest.mark.asyncio 

61 async def test_get(self, app_client: TestClient, auth_header): 

62 o = db.Organization(organizationName=self.name) 

63 await o.save() 

64 # Done setup 

65 

66 response = app_client.get(self.endpoint, headers=auth_header) 

67 

68 assert ( 

69 response.status_code == 200 

70 ), "Invalid response code when getting route with organizations present" 

71 

72 assert ( 

73 len(response.json()["organizations"]) == 1 

74 ), "Invalid payload lenght when querying organizations" 

75 

76 assert ( 

77 response.json()["organizations"][0]["organizationName"] == self.name 

78 ), "Invalid Organization name"