Coverage for tests/conftest.py: 100%

40 statements  

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

1from datetime import datetime 

2 

3import pytest 

4from beanie import PydanticObjectId 

5from fastapi.testclient import TestClient 

6from mock import patch 

7from pydantic import BaseModel, Field 

8 

9from microservice_websocket.app.blueprints.api.test import test_router 

10 

11# @pytest.fixture() 

12# def isoTimestamp() -> str: 

13# """ 

14# Time format received from chirpstack. 

15# It does follow the ISO8601 standard. 

16# """ 

17# return "2013-03-31T16:21:17.528002Z" 

18# 

19# 

20# @pytest.fixture() 

21# def node_data() -> dict: 

22# return { 

23# "sensorID": 1, 

24# "sensorName": "irma-sensor", 

25# "applicationID": "foo", 

26# "organizationID": "bar", 

27# "data": { 

28# "state": 3, 

29# "sensorData": 4.5, 

30# "mobius_sensorId": "foo", 

31# "mobius_sensorPath": "bar", 

32# }, 

33# "publishedAt": "time", 

34# "payloadType": 1, 

35# } 

36 

37 

38# @pytest.fixture() 

39# def testing_socketio() -> Client: 

40# return socketio 

41 

42 

43@pytest.fixture 

44def app_client() -> TestClient: 

45 with patch("microservice_websocket.app.config.TESTING", True): 

46 

47 from microservice_websocket.app import app 

48 

49 app.include_router(test_router) 

50 with TestClient(app) as client: 

51 return client 

52 

53 

54# @pytest.fixture() 

55# def socketio_client( 

56# testing_app: Flask, app_client: FlaskClient, testing_socketio: SocketIO 

57# ) -> SocketIOTestClient: 

58# return socketio.test_client(testing_app, flask_test_client=app_client) 

59 

60 

61@pytest.fixture 

62def token(app_client: TestClient) -> str: 

63 response = app_client.post( 

64 "/api/jwt/", 

65 json={"email": "foo@bar.com", "password": "baz"}, 

66 ) 

67 

68 decoded_json = response.json() 

69 

70 return decoded_json["access_token"] 

71 

72 

73@pytest.fixture() 

74def auth_header(token: str): 

75 return {"Authorization": f"Bearer {token}"} 

76 

77 

78# @pytest.fixture() 

79# def refresh_header(tokens): 

80# return {"Authorization": f"Bearer {tokens[1]}"} 

81 

82 

83@pytest.fixture() 

84def obj_id() -> str: 

85 return "63186eab0ca2d54a5c258384" 

86 

87 

88class MockReading(BaseModel): 

89 id: PydanticObjectId 

90 node: PydanticObjectId 

91 canID: int = Field(default=..., lt=5, gt=0) 

92 sensorNumber: int = Field(default=..., lt=3, gt=0) 

93 readingID: int 

94 sessionID: int 

95 dangerLevel: int = 0 

96 window1: int = 0 

97 window2: int = 0 

98 window3: int = 0 

99 publishedAt: datetime 

100 

101 

102@pytest.fixture() 

103def reading(obj_id: str) -> MockReading: 

104 return MockReading( 

105 id=PydanticObjectId(obj_id), 

106 node=PydanticObjectId(obj_id), 

107 canID=2, 

108 sensorNumber=1, 

109 readingID=1_640_200, 

110 sessionID=1_640_000, 

111 dangerLevel=4, 

112 window1=111, 

113 window2=222, 

114 window3=333, 

115 publishedAt=datetime.now(), 

116 )