Coverage for tests/store/test_get_publisher_details.py: 96%

52 statements  

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

1from unittest import TestCase 

2from unittest.mock import patch 

3from webapp.app import app 

4from webapp.helpers import get_soup 

5 

6api_publisher_response = { 

7 "results": [ 

8 { 

9 "id": "3uPxmv77o1PrixpQFIf8o7SkOLsnMWmZ", 

10 "name": "kafka", 

11 "type": "charm", 

12 "result": { 

13 "deployable-on": [], 

14 "description": "Apache Kafka is ...", 

15 "media": [ 

16 { 

17 "height": None, 

18 "type": "icon", 

19 "url": "https://icon-url.com/path/icon.png", 

20 "width": None, 

21 } 

22 ], 

23 "publisher": { 

24 "display-name": "Canonical", 

25 "id": "gVVkuxw4C56POG1eszt4RPR3L5Eak8XE", 

26 "username": "data-platform", 

27 "validation": "unproven", 

28 }, 

29 "summary": "Charmed Apache Kafka Operator", 

30 "title": "Apache Kafka", 

31 }, 

32 } 

33 ] 

34} 

35 

36 

37class TestGetPublisherDetails(TestCase): 

38 def setUp(self): 

39 app.testing = True 

40 self.client = app.test_client() 

41 

42 @patch("webapp.store_api.device_gateway.find") 

43 def test_get_publisher_details(self, mock_find): 

44 mock_find.return_value = api_publisher_response 

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

46 main_content = get_soup(response.text).body.find( 

47 "main", attrs={"id": "main-content"} 

48 ) 

49 self.assertEqual(response.status_code, 200) 

50 self._assert_heading_section(main_content, "Canonical") 

51 self._assert_content_section( 

52 main_content, 

53 { 

54 "message": "Showing 1 item", 

55 "cards_num": 1, 

56 "cards": [ 

57 { 

58 "title": "Apache Kafka", 

59 "publisher": "Canonical", 

60 "summary": "Charmed Apache Kafka Operator", 

61 } 

62 ], 

63 }, 

64 ) 

65 

66 @patch("webapp.store_api.device_gateway.find") 

67 def test_get_publisher_details_empty_response(self, mock_find): 

68 mock_find.return_value = {"results": []} 

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

70 main_content = get_soup(response.text).body.find( 

71 "main", attrs={"id": "main-content"} 

72 ) 

73 self.assertEqual(response.status_code, 200) 

74 self._assert_heading_section(main_content, "test-publisher") 

75 self._assert_content_section( 

76 main_content, 

77 {"message": "No items found", "cards_num": 0, "cards": []}, 

78 ) 

79 

80 @patch("webapp.store_api.device_gateway.find") 

81 def test_get_publisher_details_exception(self, mock_find): 

82 mock_find.side_effect = Exception("Service Unavailable") 

83 with self.assertRaises(Exception) as context: 

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

85 self.assertEqual(str(context.exception), "Service Unavailable") 

86 self.assertEqual(response.status_code, 500) 

87 

88 def _assert_heading_section(self, content, expected_publisher): 

89 # back to all charms is present and linked 

90 all_charms_elem = content.css.select( 

91 ".p-strip--dark > .u-fixed-width a" 

92 )[0] 

93 self.assertEqual(all_charms_elem.text, "< All charms") 

94 self.assertEqual(all_charms_elem.get("href"), "/") 

95 # publisher name title is present and equal 

96 publisher_elem = content.css.select( 

97 ".p-strip--dark > .u-fixed-width .p-heading--2" 

98 )[0] 

99 self.assertEqual(publisher_elem.text, expected_publisher) 

100 

101 def _assert_content_section(self, content, expected_values): 

102 # message is present and correct 

103 message_elem = content.css.select(".p-strip > .row > p")[0] 

104 self.assertEqual(message_elem.text, expected_values["message"]) 

105 # list of cards with published charms present with all items 

106 card_elements = content.css.select(".p-strip > .row .p-card") 

107 self.assertEqual(len(card_elements), expected_values["cards_num"]) 

108 for index, card_elem in enumerate(card_elements): 

109 self._assert_card(card_elem, expected_values["cards"][index]) 

110 

111 def _assert_card(self, content, expected_values): 

112 title_elem = content.css.select("h2")[0] 

113 self.assertEqual(title_elem.text.strip(), expected_values["title"]) 

114 publisher_elem = content.css.select(".u-text--muted em")[0] 

115 self.assertEqual( 

116 publisher_elem.text.strip(), expected_values["publisher"] 

117 ) 

118 summary_elem = content.css.select("p.u-line-clamp")[0] 

119 self.assertEqual(summary_elem.text.strip(), expected_values["summary"])