Coverage for tests/api/test_github.py: 100%
54 statements
« prev ^ index » next coverage.py v7.15.0, created at 2026-07-08 12:33 +0000
« prev ^ index » next coverage.py v7.15.0, created at 2026-07-08 12:33 +0000
1from os import getenv
2from vcr_unittest import VCRTestCase
3from webapp.api.github import GitHub
4from werkzeug.exceptions import Unauthorized
7class GitHubTest(VCRTestCase):
8 def _get_vcr_kwargs(self):
9 """
10 This removes the authorization header
11 from VCR so we don't record auth parameters
12 """
13 return {"filter_headers": ["Authorization"]}
15 def setUp(self):
16 self.client = GitHub(getenv("TESTS_GITHUB_USER_TOKEN", "secret"))
17 return super(GitHubTest, self).setUp()
19 def test_get_user(self):
20 user = self.client.get_user()
21 self.assertIn("login", user)
22 self.assertIn("name", user)
23 self.assertIn("avatarUrl", user)
25 # Test Unauthorized exception when using bad credentials
26 self.client.access_token = "bad-token"
27 self.assertRaises(Unauthorized, self.client.get_user)
29 def test_get_user_repositories(self):
30 repos = self.client.get_user_repositories()
31 [self.assertIn("name", repo) for repo in repos]
33 # Test Unauthorized exception when using bad credentials
34 self.client.access_token = "bad-token"
35 self.assertRaises(Unauthorized, self.client.get_user_repositories)
37 def test_get_org_repositories(self):
38 repos = self.client.get_org_repositories("canonical-web-and-design")
39 [self.assertIn("name", repo) for repo in repos]
40 [self.assertIn("nameWithOwner", repo) for repo in repos]
41 [self.assertIn("owner", repo) for repo in repos]
43 # Test Unauthorized exception when using bad credentials
44 self.client.access_token = "bad-token"
45 self.assertRaises(Unauthorized, self.client.get_user_repositories)
47 def test_get_orgs(self):
48 orgs = self.client.get_orgs()
49 [self.assertIn("name", org) for org in orgs]
50 [self.assertIn("login", org) for org in orgs]
52 def test_check_permissions_over_repo(self):
53 # The user is the owner of the repo
54 case1 = self.client.check_permissions_over_repo(
55 "build-staging-snapcraft-io", "test1"
56 )
57 self.assertEqual(True, case1)
59 # The user doesn't have permissions for this repo
60 case2 = self.client.check_permissions_over_repo(
61 "canonical-web-and-design", "snapcraft.io"
62 )
63 self.assertEqual(False, case2)
65 def test_get_snapcraft_yaml_location(self):
66 # /snapcraft.yaml is present
67 case1 = self.client.get_snapcraft_yaml_location(
68 "build-staging-snapcraft-io", "test1"
69 )
70 self.assertEqual("snapcraft.yaml", case1)
72 # /.snapcraft.yaml is present
73 case2 = self.client.get_snapcraft_yaml_location(
74 "build-staging-snapcraft-io", "test2"
75 )
76 self.assertEqual(".snapcraft.yaml", case2)
78 # /snap/snapcraft.yaml is present
79 case3 = self.client.get_snapcraft_yaml_location(
80 "build-staging-snapcraft-io", "test3"
81 )
82 self.assertEqual("snap/snapcraft.yaml", case3)
84 # /build-aux/snap/snapcraft.yaml is present
85 case4 = self.client.get_snapcraft_yaml_location(
86 "build-staging-snapcraft-io", "test4"
87 )
88 self.assertEqual("build-aux/snap/snapcraft.yaml", case4)
90 # The repo doesn't contain a valid yaml file
91 case5 = self.client.get_snapcraft_yaml_location(
92 "build-staging-snapcraft-io", "test5"
93 )
94 self.assertEqual(False, case5)
96 def test_get_snapcraft_yaml_data(self):
97 case1 = self.client.get_snapcraft_yaml_data(
98 "build-staging-snapcraft-io", "test1"
99 )
100 self.assertEqual("test1", case1.get("name"))
102 case2 = self.client.get_snapcraft_yaml_data(
103 "build-staging-snapcraft-io", "test5"
104 )
105 self.assertEqual(None, case2.get("name"))