Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from os import getenv 

2from vcr_unittest import VCRTestCase 

3from webapp.api.github import GitHub 

4from werkzeug.exceptions import Unauthorized 

5 

6 

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"]} 

14 

15 def setUp(self): 

16 self.client = GitHub(getenv("TESTS_GITHUB_USER_TOKEN", "secret")) 

17 return super(GitHubTest, self).setUp() 

18 

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) 

24 

25 # Test Unauthorized exception when using bad credentials 

26 self.client.access_token = "bad-token" 

27 self.assertRaises(Unauthorized, self.client.get_user) 

28 

29 def test_get_user_repositories(self): 

30 repos = self.client.get_user_repositories() 

31 [self.assertIn("name", repo) for repo in repos] 

32 

33 # Test Unauthorized exception when using bad credentials 

34 self.client.access_token = "bad-token" 

35 self.assertRaises(Unauthorized, self.client.get_user_repositories) 

36 

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 

41 # Test Unauthorized exception when using bad credentials 

42 self.client.access_token = "bad-token" 

43 self.assertRaises(Unauthorized, self.client.get_user_repositories) 

44 

45 def test_get_orgs(self): 

46 orgs = self.client.get_orgs() 

47 [self.assertIn("name", org) for org in orgs] 

48 [self.assertIn("login", org) for org in orgs] 

49 

50 def test_check_permissions_over_repo(self): 

51 # The user is the owner of the repo 

52 case1 = self.client.check_permissions_over_repo( 

53 "build-staging-snapcraft-io", "test1" 

54 ) 

55 self.assertEqual(True, case1) 

56 

57 # The user doesn't have permissions for this repo 

58 case2 = self.client.check_permissions_over_repo( 

59 "canonical-web-and-design", "snapcraft.io" 

60 ) 

61 self.assertEqual(False, case2) 

62 

63 def test_get_snapcraft_yaml_location(self): 

64 # /snapcraft.yaml is present 

65 case1 = self.client.get_snapcraft_yaml_location( 

66 "build-staging-snapcraft-io", "test1" 

67 ) 

68 self.assertEqual("snapcraft.yaml", case1) 

69 

70 # /.snapcraft.yaml is present 

71 case2 = self.client.get_snapcraft_yaml_location( 

72 "build-staging-snapcraft-io", "test2" 

73 ) 

74 self.assertEqual(".snapcraft.yaml", case2) 

75 

76 # /snap/snapcraft.yaml is present 

77 case3 = self.client.get_snapcraft_yaml_location( 

78 "build-staging-snapcraft-io", "test3" 

79 ) 

80 self.assertEqual("snap/snapcraft.yaml", case3) 

81 

82 # /build-aux/snap/snapcraft.yaml is present 

83 case4 = self.client.get_snapcraft_yaml_location( 

84 "build-staging-snapcraft-io", "test4" 

85 ) 

86 self.assertEqual("build-aux/snap/snapcraft.yaml", case4) 

87 

88 # The repo doesn't contain a valid yaml file 

89 case5 = self.client.get_snapcraft_yaml_location( 

90 "build-staging-snapcraft-io", "test5" 

91 ) 

92 self.assertEqual(False, case5) 

93 

94 def test_get_snapcraft_yaml_data(self): 

95 case1 = self.client.get_snapcraft_yaml_data( 

96 "build-staging-snapcraft-io", "test1" 

97 ) 

98 self.assertEqual("test1", case1.get("name")) 

99 

100 case2 = self.client.get_snapcraft_yaml_data( 

101 "build-staging-snapcraft-io", "test5" 

102 ) 

103 self.assertEqual(None, case2.get("name"))