Coverage for tests/publisher/snaps/tests_post_listing.py: 100%

48 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-28 22:05 +0000

1import json 

2 

3import responses 

4from tests.publisher.endpoint_testing import BaseTestCases 

5 

6 

7class PostListingPageNotAuth(BaseTestCases.EndpointLoggedOut): 

8 def setUp(self): 

9 snap_name = "test-snap" 

10 endpoint_url = "/api/{}/listing".format(snap_name) 

11 

12 super().setUp( 

13 snap_name=snap_name, 

14 endpoint_url=endpoint_url, 

15 method_endpoint="POST", 

16 ) 

17 

18 

19class PostMetadataListingPage(BaseTestCases.EndpointLoggedIn): 

20 def setUp(self): 

21 self.snap_id = "complexId" 

22 

23 snap_name = "test-snap" 

24 endpoint_url = "/api/{}/listing".format(snap_name) 

25 api_url = ( 

26 "https://dashboard.snapcraft.io/dev/api/" 

27 "snaps/{}/metadata?conflict_on_update=true" 

28 ).format(self.snap_id) 

29 

30 changes = {"description": "New description"} 

31 data = {"changes": json.dumps(changes), "snap_id": self.snap_id} 

32 

33 super().setUp( 

34 snap_name=snap_name, 

35 api_url=api_url, 

36 endpoint_url=endpoint_url, 

37 method_api="PUT", 

38 method_endpoint="POST", 

39 data=data, 

40 ) 

41 

42 @responses.activate 

43 def test_post_no_data(self): 

44 response = self.client.post(self.endpoint_url) 

45 

46 assert response.status_code == 200 

47 

48 @responses.activate 

49 def test_update_invalid_field(self): 

50 response = self.client.post( 

51 self.endpoint_url, 

52 data={"changes": '{"dumb_field": "this is a dumb field"}'}, 

53 ) 

54 

55 assert 0 == len(responses.calls) 

56 assert response.status_code == 200 

57 

58 @responses.activate 

59 def test_update_valid_field(self): 

60 responses.add(responses.PUT, self.api_url, json={}, status=200) 

61 

62 changes = {"description": "New description"} 

63 

64 response = self.client.post( 

65 self.endpoint_url, 

66 data={"changes": json.dumps(changes), "snap_id": self.snap_id}, 

67 ) 

68 

69 self.assertEqual(1, len(responses.calls)) 

70 called = responses.calls[0] 

71 self.assertEqual(self.api_url, called.request.url) 

72 self.assertEqual( 

73 self.authorization, called.request.headers.get("Authorization") 

74 ) 

75 self.assertEqual( 

76 b'{"description": "New description"}', 

77 called.request.body, 

78 ) 

79 

80 assert response.status_code == 200 

81 

82 @responses.activate 

83 def test_update_description_with_carriage_return(self): 

84 responses.add(responses.PUT, self.api_url, json={}, status=200) 

85 

86 changes = {"description": "This is a description\r\n"} 

87 

88 response = self.client.post( 

89 self.endpoint_url, 

90 data={"changes": json.dumps(changes), "snap_id": self.snap_id}, 

91 ) 

92 

93 self.assertEqual(1, len(responses.calls)) 

94 called = responses.calls[0] 

95 self.assertEqual(self.api_url, called.request.url) 

96 self.assertEqual( 

97 self.authorization, called.request.headers.get("Authorization") 

98 ) 

99 self.assertEqual( 

100 b'{"description": "This is a description\\n"}', 

101 called.request.body, 

102 ) 

103 

104 assert response.status_code == 200