Coverage for tests/publisher/tests_publisher.py: 100%

93 statements  

« prev     ^ index     » next       coverage.py v7.15.0, created at 2026-07-08 12:33 +0000

1from requests.exceptions import ConnectionError 

2 

3import responses 

4from flask_testing import TestCase 

5from tests.publisher.endpoint_testing import BaseTestCases 

6from webapp.app import create_app 

7from webapp.authentication import get_authorization_header 

8 

9# Make sure tests fail on stray responses. 

10responses.mock.assert_all_requests_are_fired = True 

11 

12 

13class TestCache(BaseTestCases.EndpointLoggedInErrorHandling): 

14 def setUp(self): 

15 api_url = "https://dashboard.snapcraft.io/dev/api/account" 

16 endpoint_url = "/account/publisher" 

17 

18 super().setUp( 

19 snap_name=None, 

20 endpoint_url=endpoint_url, 

21 method_endpoint="GET", 

22 api_url=api_url, 

23 method_api="GET", 

24 ) 

25 

26 @responses.activate 

27 def test_cache_disabled(self): 

28 responses.add( 

29 responses.GET, 

30 url="https://066-eov-335.mktorest.com/identity/oauth/token" 

31 "?grant_type=client_credentials&client_id=fake_id&" 

32 "client_secret=fake_secret", 

33 json={}, 

34 status=200, 

35 ) 

36 responses.add(responses.GET, self.api_url, json={}, status=200) 

37 

38 response = self.client.get(self.endpoint_url) 

39 self.assertEqual(200, response.status_code) 

40 

41 responses.replace(responses.GET, self.api_url, body=ConnectionError()) 

42 response = self.client.get(self.endpoint_url) 

43 self.assertEqual(response.status_code, 502) 

44 

45 

46class PublisherPage(TestCase): 

47 def create_app(self): 

48 app = create_app(testing=True) 

49 app.secret_key = "secret_key" 

50 app.config["WTF_CSRF_METHODS"] = [] 

51 

52 return app 

53 

54 def test_account(self): 

55 local_redirect = self.client.get("/account") 

56 redirect_url = "/login?next=/account" 

57 assert local_redirect.status_code == 302 

58 assert local_redirect.headers.get("Location") == redirect_url 

59 

60 def _log_in(self, client): 

61 """Emulates test client login in the store. 

62 

63 Fill current session with `openid` and `macaroon_exchanged`. 

64 

65 Return the expected `Authorization` header for further verification in 

66 API requests. 

67 """ 

68 exchanged_macaroon = "test-exchanged-macaroon" 

69 

70 with client.session_transaction() as s: 

71 s["publisher"] = { 

72 "image": None, 

73 "nickname": "Toto", 

74 "fullname": "El Toto", 

75 "email": "test@example.com", 

76 } 

77 s["macaroon_exchanged"] = exchanged_macaroon 

78 

79 return get_authorization_header(exchanged_macaroon) 

80 

81 def test_username_not_logged_in(self): 

82 response = self.client.get("/account/username") 

83 self.assertEqual(302, response.status_code) 

84 self.assertEqual("/login?next=/account/username", response.location) 

85 

86 def test_account_not_logged_in(self): 

87 response = self.client.get("/account") 

88 self.assertEqual(302, response.status_code) 

89 self.assertEqual("/login?next=/account", response.location) 

90 

91 # /account endpoint 

92 # === 

93 @responses.activate 

94 def test_account_redirect(self): 

95 self._log_in(self.client) 

96 response = self.client.get("/account") 

97 self.assertEqual(302, response.status_code) 

98 self.assertEqual("/snaps", response.location) 

99 

100 # /account/username endpoint 

101 # === 

102 @responses.activate 

103 def test_username_logged_in(self): 

104 self._log_in(self.client) 

105 response = self.client.get("/account/username") 

106 

107 assert response.status_code == 200 

108 self.assert_template_used("publisher/username.html") 

109 

110 @responses.activate 

111 def test_post_username_logged_in(self): 

112 responses.add( 

113 responses.PATCH, 

114 "https://dashboard.snapcraft.io/dev/api/account", 

115 json={}, 

116 status=200, 

117 ) 

118 

119 authorization = self._log_in(self.client) 

120 response = self.client.post( 

121 "/account/username", data={"username": "toto"} 

122 ) 

123 

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

125 called = responses.calls[0] 

126 self.assertEqual( 

127 "https://dashboard.snapcraft.io/dev/api/account", 

128 called.request.url, 

129 ) 

130 self.assertEqual( 

131 authorization, called.request.headers.get("Authorization") 

132 ) 

133 self.assertEqual(called.response.json(), {}) 

134 self.assertEqual(b'{"short_namespace": "toto"}', called.request.body) 

135 

136 self.assertEqual(302, response.status_code) 

137 self.assertEqual("/account/", response.location) 

138 

139 @responses.activate 

140 def test_post_no_username_logged_in(self): 

141 self._log_in(self.client) 

142 response = self.client.post("/account/username") 

143 

144 self.assertEqual(0, len(responses.calls)) 

145 

146 self.assertEqual(302, response.status_code) 

147 self.assertEqual("/account/username", response.location) 

148 

149 @responses.activate 

150 def test_post_bad_username_logged_in(self): 

151 payload = { 

152 "error_list": [ 

153 {"code": "custom-error", "message": "great message"} 

154 ] 

155 } 

156 responses.add( 

157 responses.PATCH, 

158 "https://dashboard.snapcraft.io/dev/api/account", 

159 json=payload, 

160 status=400, 

161 ) 

162 

163 authorization = self._log_in(self.client) 

164 response = self.client.post( 

165 "/account/username", data={"username": "toto"} 

166 ) 

167 

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

169 called = responses.calls[0] 

170 self.assertEqual( 

171 "https://dashboard.snapcraft.io/dev/api/account", 

172 called.request.url, 

173 ) 

174 self.assertEqual( 

175 authorization, called.request.headers.get("Authorization") 

176 ) 

177 self.assertEqual(b'{"short_namespace": "toto"}', called.request.body) 

178 

179 assert response.status_code == 200 

180 self.assert_template_used("publisher/username.html") 

181 self.assert_context("username", "toto") 

182 self.assert_context("error_list", payload["error_list"])