Coverage for tests/interfaces/test_single_interface.py: 100%

32 statements  

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

1import json 

2from unittest.mock import patch 

3from unittest import TestCase 

4import responses 

5from webapp.app import app 

6from webapp.integrations.logic import Interfaces 

7 

8 

9class TestSingleInterface(TestCase): 

10 def setUp(self): 

11 app.config["TESTING"] = True 

12 app.config["DEBUG"] = True 

13 self.client = app.test_client() 

14 self.test_interfaces = Interfaces() 

15 

16 self.github_interfaces_url = "".join( 

17 [ 

18 "https://api.github.com/repos/canonical/", 

19 "charm-relation-interfaces/contents/interfaces", 

20 ] 

21 ) 

22 self.charmhub_api_get_requirers = "".join( 

23 [ 

24 "https://api.charmhub.io/v2/charms/find?", 

25 "q=&category=&publisher=&requires=test_interface", 

26 ] 

27 ) 

28 self.charmhub_api_get_providers = "".join( 

29 [ 

30 "https://api.charmhub.io/v2/charms/find?", 

31 "q=&category=&publisher=&provides=test_interface", 

32 ] 

33 ) 

34 

35 @responses.activate 

36 def test_single_interface(self): 

37 responses.add( 

38 responses.Response( 

39 method="GET", 

40 url=f"{self.github_interfaces_url}/test_interface", 

41 body="test_interface", 

42 status=200, 

43 ) 

44 ) 

45 responses.add( 

46 responses.Response( 

47 method="GET", 

48 url=self.charmhub_api_get_requirers, 

49 body=json.dumps({"results": ["test_interface1"]}), 

50 status=200, 

51 ) 

52 ) 

53 responses.add( 

54 responses.Response( 

55 method="GET", 

56 url=self.charmhub_api_get_providers, 

57 body=json.dumps({"results": ["test_interface2"]}), 

58 status=200, 

59 ) 

60 ) 

61 response = self.client.get("/integrations/test_interface.json") 

62 self.assertEqual(response.status_code, 200) 

63 self.assertDictEqual( 

64 response.json, 

65 { 

66 "other_charms": { 

67 "providers": ["test_interface2"], 

68 "requirers": ["test_interface1"], 

69 } 

70 }, 

71 ) 

72 

73 @patch("webapp.integrations.logic.Interfaces") 

74 @patch("canonicalwebteam.store_api.publishergw.PublisherGW.find") 

75 def test_repo_has_no_interface(self, mock_find, mock_interfaces): 

76 mock_find.return_value = { 

77 "results": ["mock_providers", "mock_requirers"] 

78 } 

79 

80 mock_interface_logic = mock_interfaces() 

81 mock_interface_logic.get_interface_list.return_value = [] 

82 response = self.client.get("/integrations/test_interface.json") 

83 self.assertEqual(response.status_code, 200) 

84 self.assertIn(b"other_charms", response.data)