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

46 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 TestGetApplications: 

8 endpoint = "/api/applications" 

9 name = "appName" 

10 

11 # Getting empty Applications with no args 

12 def test_get_no_args(self, app_client: TestClient, auth_header): 

13 

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

15 

16 assert ( 

17 response.status_code == 422 

18 ), "Invalid response code when getting Application with no args" 

19 

20 # Getting created application 

21 @pytest.mark.asyncio 

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

23 o = db.Organization(organizationName="orgName") 

24 await o.save() 

25 a = db.Application(applicationName=self.name, organization=o.id) 

26 await a.save() 

27 # Done setup 

28 

29 response = app_client.get( 

30 self.endpoint + f"?organizationID={str(o.id)}", 

31 headers=auth_header, 

32 ) 

33 

34 assert ( 

35 response.status_code == 200 

36 ), "Invalid response code when getting Application" 

37 

38 assert ( 

39 response.json()["applications"][0]["applicationName"] == self.name 

40 ), "Invalid Application name" 

41 

42 

43class TestPostApplications: 

44 endpoint = "/api/application" 

45 name = "appName" 

46 

47 # Creating Application 

48 @pytest.mark.asyncio 

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

50 o = db.Organization(organizationName="orgName") 

51 await o.save() 

52 # Done setup 

53 

54 response = app_client.post( 

55 self.endpoint, 

56 json={"name": self.name, "organizationID": str(o.id)}, 

57 headers=auth_header, 

58 ) 

59 

60 assert ( 

61 response.status_code == 200 

62 ), "Invalid response code when posting valid data in create route" 

63 

64 assert ( 

65 app := await db.Application.find_one() 

66 ) and app.applicationName == self.name, "Invalid name for Application object" 

67 

68 # Creating application with wrong orgID 

69 def test_create_with_no_org(self, app_client: TestClient, auth_header, obj_id): 

70 response = app_client.post( 

71 self.endpoint, 

72 json={"name": "qux", "organizationID": obj_id}, 

73 headers=auth_header, 

74 ) 

75 

76 assert ( 

77 response.status_code == 404 

78 ), "Invalid response code when posting data to wrong orgID" 

79 

80 # Creating application with same name 

81 @pytest.mark.asyncio 

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

83 o = db.Organization(organizationName="orgName") 

84 await o.save() 

85 a = db.Application(applicationName=self.name, organization=o.id) 

86 await a.save() 

87 # Done setup 

88 

89 response = app_client.post( 

90 self.endpoint, 

91 json={"name": self.name, "organizationID": str(o.id)}, 

92 headers=auth_header, 

93 ) 

94 print(await db.Application.find_all().to_list()) 

95 

96 assert ( 

97 response.status_code == 400 

98 ), "Invalid response code when posting data with already existing name" 

99 

100 # Creating application with invalid payload 

101 @pytest.mark.asyncio 

102 async def test_create_invalid_payload(self, app_client: TestClient, auth_header): 

103 o = db.Organization(organizationName="orgName") 

104 await o.save() 

105 # Done setup 

106 

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

108 

109 assert ( 

110 response.status_code == 422 

111 ), "Invalid response code when posting invalid data"