diff --git a/master/buildbot/test/unit/test_www_oauth.py b/master/buildbot/test/unit/test_www_oauth.py index 748eec71da3..94c404ce406 100644 --- a/master/buildbot/test/unit/test_www_oauth.py +++ b/master/buildbot/test/unit/test_www_oauth.py @@ -62,8 +62,9 @@ def setUp(self): self.githubAuth = oauth2.GitHubAuth("ghclientID", "clientSECRET") self.gitlabAuth = oauth2.GitLabAuth( "https://gitlab.test/", "glclientID", "clientSECRET") + self.bitbucketAuth = oauth2.BitbucketAuth("bbclientID", "clientSECRET") - for auth in [self.googleAuth, self.githubAuth, self.gitlabAuth]: + for auth in [self.googleAuth, self.githubAuth, self.gitlabAuth, self.bitbucketAuth]: self._master = master = self.make_master(url='h:/a/b/', auth=auth) auth.reconfigAuth(master, master.config) @@ -110,6 +111,18 @@ def test_getGitLabLoginURL(self): "&client_id=glclientID") self.assertEqual(res, exp) + @defer.inlineCallbacks + def test_getBitbucketLoginURL(self): + res = yield self.bitbucketAuth.getLoginURL('http://redir') + exp = ("https://bitbucket.org/site/oauth2/authorize?state=redirect%3D" + "http%253A%252F%252Fredir&redirect_uri=h%3A%2Fa%2Fb%2Fauth%2F" + "login&response_type=code&client_id=bbclientID") + self.assertEqual(res, exp) + res = yield self.bitbucketAuth.getLoginURL(None) + exp = ("https://bitbucket.org/site/oauth2/authorize?redirect_uri=h%3A%2" + "Fa%2Fb%2Fauth%2Flogin&response_type=code&client_id=bbclientID") + self.assertEqual(res, exp) + @defer.inlineCallbacks def test_GoogleVerifyCode(self): requests.get.side_effect = [] @@ -169,6 +182,30 @@ def test_GitlabVerifyCode(self): "avatar_url": "https://avatar/fbar.png", "groups": ["hello", "grp"]}, res) + @defer.inlineCallbacks + def test_BitbucketVerifyCode(self): + requests.get.side_effect = [] + requests.post.side_effect = [ + FakeResponse(dict(access_token="TOK3N"))] + self.bitbucketAuth.get = mock.Mock(side_effect=[ + dict( # /user + username="bar", + display_name="foo bar"), + dict( # /user/emails + values=[ + {'email': 'buzz@bar', 'is_primary': False}, + {'email': 'bar@foo', 'is_primary': True}]), + dict( # /teams?role=member + values=[ + {'username': 'hello'}, + {'username': 'grp'}]) + ]) + res = yield self.bitbucketAuth.verifyCode("code!") + self.assertEqual({'email': 'bar@foo', + 'username': 'bar', + "groups": ["hello", "grp"], + 'full_name': 'foo bar'}, res) + @defer.inlineCallbacks def test_loginResource(self): class fakeAuth(object): @@ -199,6 +236,8 @@ def test_getConfig(self): 'name': 'Google', 'oauth2': True}) self.assertEqual(self.gitlabAuth.getConfigDict(), {'fa_icon': 'fa-git', 'autologin': False, 'name': 'GitLab', 'oauth2': True}) + self.assertEqual(self.bitbucketAuth.getConfigDict(), {'fa_icon': 'fa-bitbucket', 'autologin': False, + 'name': 'Bitbucket', 'oauth2': True}) # unit tests are not very usefull to write new oauth support # so following is an e2e test, which opens a browser, and do the oauth