Coverage for tests/login/test_login.py: 100%

38 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-27 22:07 +0000

1import unittest 

2from flask import session 

3 

4import talisker 

5from canonicalwebteam.candid import CandidClient 

6from webapp.app import app 

7import responses 

8 

9request_session = talisker.requests.get_session() 

10candid = CandidClient(request_session) 

11 

12 

13class TestLoginViews(unittest.TestCase): 

14 def setUp(self): 

15 self.app = app 

16 app.config["TESTING"] = True 

17 self.app.config["WTF_CSRF_ENABLED"] = [] 

18 

19 self.client = self.app.test_client() 

20 self.api_url = "https://api.charmhub.io/v1/tokens" 

21 

22 def test_login(self): 

23 with self.client as client: 

24 res = client.get("/login") 

25 self.assertEqual(res.status_code, 302) 

26 self.assertIn("account-macaroon", session) 

27 

28 def test_login_next(self): 

29 with self.client as client: 

30 client.get("/login?next=/test-page") 

31 self.assertIn("next_url", session) 

32 self.assertEqual(session["next_url"], "/test-page") 

33 

34 @responses.activate 

35 def test_login_api_500(self): 

36 responses.add( 

37 responses.Response(method="POST", url=self.api_url, status=500) 

38 ) 

39 

40 response = self.client.get("/login") 

41 

42 assert len(responses.calls) == 1 

43 assert response.status_code == 502 

44 

45 def test_logout(self): 

46 response = self.client.get("/logout") 

47 self.assertEqual(response.status_code, 302) 

48 with self.client.session_transaction() as s: 

49 self.assertEqual(response.location, "/") 

50 self.assertEqual(s.get("account-auth"), None) 

51 self.assertEqual(s.get("account-macaroon"), None)