Coverage for tests/store/tests_status_banner.py: 100%

38 statements  

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

1import responses 

2from unittest.mock import patch 

3from flask import render_template 

4from flask_testing import TestCase 

5from webapp.app import create_app 

6 

7SNAP_FIND_RESPONSE = { 

8 "results": [ 

9 { 

10 "name": "test-snap", 

11 "snap": { 

12 "media": [], 

13 "publisher": { 

14 "display-name": "Test Publisher", 

15 "id": "test-publisher-id", 

16 "username": "test-publisher", 

17 "validation": "unproven", 

18 }, 

19 "summary": "A test snap", 

20 "title": "Test Snap", 

21 }, 

22 "snap-id": "test-snap-id", 

23 } 

24 ] 

25} 

26 

27 

28class StatusBannerTest(TestCase): 

29 def create_app(self): 

30 app = create_app(testing=True) 

31 app.secret_key = "secret_key" 

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

33 return app 

34 

35 @responses.activate 

36 def test_banner_shown_when_status_banner_set(self): 

37 message = "Temporary performance degradation in progress." 

38 with patch("webapp.handlers.STATUS_BANNER", message): 

39 responses.add( 

40 responses.GET, 

41 "https://api.snapcraft.io/v2/snaps/find", 

42 json=SNAP_FIND_RESPONSE, 

43 status=200, 

44 ) 

45 response = self.client.get("/publisher/test-publisher") 

46 self.assertEqual(response.status_code, 200) 

47 body = response.get_data(as_text=True) 

48 self.assertIn("p-notification--caution", body) 

49 self.assertIn(message, body) 

50 

51 @responses.activate 

52 def test_banner_hidden_when_status_banner_empty(self): 

53 with patch("webapp.handlers.STATUS_BANNER", ""): 

54 responses.add( 

55 responses.GET, 

56 "https://api.snapcraft.io/v2/snaps/find", 

57 json=SNAP_FIND_RESPONSE, 

58 status=200, 

59 ) 

60 response = self.client.get("/publisher/test-publisher") 

61 self.assertEqual(response.status_code, 200) 

62 body = response.get_data(as_text=True) 

63 self.assertNotIn('id="status-banner"', body) 

64 

65 def test_banner_shown_in_admin_layout(self): 

66 message = "Admin maintenance notice." 

67 with patch("webapp.handlers.STATUS_BANNER", message): 

68 with self.app.test_request_context("/admin"): 

69 body = render_template( 

70 "admin/admin.html", 

71 api_url="https://dashboard.snapcraft.io/", 

72 ) 

73 self.assertIn('id="status-banner"', body) 

74 self.assertIn(message, body) 

75 self.assertIn("z-index: 201;", body)