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

48 statements  

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

1import responses 

2from tests.publisher.endpoint_testing import BaseTestCases 

3 

4 

5class GetUsernamePageNotAuth(BaseTestCases.EndpointLoggedOut): 

6 def setUp(self): 

7 endpoint_url = "/account/username" 

8 super().setUp(snap_name=None, endpoint_url=endpoint_url) 

9 

10 

11class GetUsernamePage(BaseTestCases.BaseAppTesting): 

12 def setUp(self): 

13 endpoint_url = "/account/username" 

14 super().setUp(snap_name=None, api_url=None, endpoint_url=endpoint_url) 

15 

16 @responses.activate 

17 def test_agreement_logged_in(self): 

18 self._log_in(self.client) 

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

20 

21 assert response.status_code == 200 

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

23 

24 

25class PostUsernamePageNotAuth(BaseTestCases.EndpointLoggedOut): 

26 def setUp(self): 

27 endpoint_url = "/account/username" 

28 super().setUp( 

29 snap_name=None, endpoint_url=endpoint_url, method_endpoint="POST" 

30 ) 

31 

32 

33class PostUsernamePage(BaseTestCases.EndpointLoggedIn): 

34 def setUp(self): 

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

36 data = {"username": "toto"} 

37 endpoint_url = "/account/username" 

38 

39 super().setUp( 

40 snap_name=None, 

41 api_url=api_url, 

42 endpoint_url=endpoint_url, 

43 method_endpoint="POST", 

44 method_api="PATCH", 

45 data=data, 

46 ) 

47 

48 @responses.activate 

49 def test_post_username(self): 

50 responses.add(responses.PATCH, self.api_url, json={}, status=200) 

51 

52 response = self.client.post(self.endpoint_url, data=self.data) 

53 

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

55 called = responses.calls[0] 

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

57 self.assertEqual( 

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

59 ) 

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

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

62 

63 self.assertEqual(302, response.status_code) 

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

65 

66 @responses.activate 

67 def test_post_username_empty(self): 

68 response = self.client.post(self.endpoint_url, data={"username": ""}) 

69 

70 self.assertEqual(302, response.status_code) 

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

72 

73 @responses.activate 

74 def test_post_no_data(self): 

75 response = self.client.post(self.endpoint_url, data={}) 

76 

77 self.assertEqual(302, response.status_code) 

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