# This file is part of Buildbot.  Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

from twisted.internet import defer
from twisted.trial import unittest

from buildbot.test.fake import httpclientservice as fakehttpclientservice
from buildbot.test.util import www
from buildbot.test.util.misc import TestReactorMixin
from buildbot.www import auth
from buildbot.www import avatar


class TestAvatar(avatar.AvatarBase):

    def getUserAvatar(self, email, username, size, defaultAvatarUrl):
        return defer.succeed((b"image/png", '{!r} {!r} {!r}'.format(
                                email, size, defaultAvatarUrl).encode('utf-8')))


class AvatarResource(TestReactorMixin, www.WwwTestMixin, unittest.TestCase):

    def setUp(self):
        self.setUpTestReactor()

    @defe
r.inlineCallbacks
    def test_default(self):
        master = self.make_master(
            url='http://a/b/', auth=auth.NoAuth(), avatar_methods=[])
        rsrc = avatar.AvatarResource(master)
        rsrc.reconfigResource(master.config)

        res = yield self.render_resource(rsrc, b'/')
        self.assertEqual(
            res, dict(redirected=avatar.AvatarResource.defaultAvatarUrl))

    @defer.inlineCallbacks
    def test_gravatar(self):
        master = 
self.make_master(
            url='http://a/b/', auth=auth.NoAuth(), avatar_methods=[avatar.AvatarGravatar()])
        rsrc = avatar.AvatarResource(master)
        rsrc.reconfigResource(master.config)

        res = yield self.render_resource(rsrc, b'/?email=foo')
        self.assertEqual(res, dict(redirected=b'//www.gravatar.com/avatar/acbd18db4cc2f85ce'
                                   b'def654fccc4a4d8?d=retro&s=32'))

    @defer.inlineCallbacks
    def test_avatar_call(self):
        master = self.make_master(
            url='http://a/b/', auth=auth.NoAuth(), avatar_methods=[TestAvatar()])
        rsrc = avatar.AvatarResource(master)
        rsrc.reconfigResource(master.config)

        res = yield self.render_resource(rsrc, b'/?email=foo')
        self.assertEqual(res, b"b'foo' 32 b'http://a/b/img/nobody.png'")

    @defer.inlineCallbacks
    def test_custom_size(self):
        master = self.make_master(
            url='http://a/b/', auth=auth.NoAuth(), avatar_methods=[TestAvatar()])
        rsrc = avatar.AvatarResource(master)
        rsrc.reconfigResource(master.config)

        res = yield self.render_resource(rsrc, b'/?email=foo&size=64')
        self.assertEqual(res, b"b'foo' 64 b'http://a/b/img/nobody.png'")

    @defer.inlineCallbacks
    def test_invalid_size(self):
        master = self.make_master(
            url='http://a/b/', auth=auth.NoAuth(), avatar_methods=[TestAvatar()])
        rsrc = avatar.AvatarResource(master)
        rsrc.reconfigResource(master.config)

        res = yield self.render_resource(rsrc, b'/?email=foo&size=abcd')
        self.assertEqual(res, b"b'foo' 32 b'http://a/b/img/nobody.png'")

    @defer.inlineCallbacks
    def test_custom_not_found(self):
        # use gravatar if the custom avatar fail to return a response
        class CustomAvatar(avatar.AvatarBase):

            def getUserAvatar(self, email, username, size, defaultAvatarUrl):
                return defer.succeed(None)

        master = self.make_master(url=b'http://a/b/', auth=auth.NoAuth(),
                                  avatar_methods=[CustomAvatar(), avatar.AvatarGravatar()])
        rsrc = avatar.AvatarResource(master)
        rsrc.reconfigResource(master.config)

        res = yield self.render_resource(rsrc, b'/?email=foo')
        self.assertEqual(res, dict(redirected=b'//www.gravatar.com/avatar/acbd18db4cc2f85ce'
                         b'def654fccc4a4d8?d=retro&s=32'))


github_username_search_reply = {
    "login": "defunkt",
    "id": 42424242,
    "node_id": "MDQ6VXNlcjQyNDI0MjQy",
    "avatar_url": "https://avatars3.githubusercontent.com/u/42424242?v=4",
    "gravatar_id": "",
    "url": "https://api.github.com/users/defunkt",
    "html_url": "https://github.com/defunkt",
    "followers_url": "https://api.github.com/users/defunkt/followers",
    "following_url": "https://api.github.com/users/defunkt/following{/other_user}",
    "gists_url": "https://api.github.com/users/defunkt/gists{/gist_id}",
    "starred_url": "https://api.github.com/users/defunkt/starred{/owner}{/repo}",
    "subscriptions_url": "https://api.github.com/users/defunkt/subscriptions",
    "organizations_url": "https://api.github.com/users/defunkt/orgs",
    "repos_url": "https://api.github.com/users/defunkt/repos",
    "events_url": "https://api.github.com/users/defunkt/events{/privacy}",
    "received_events_url": "https://api.github.com/users/defunkt/received_events",
    "type": "User",
    "site_admin": False,
    "name": "Defunkt User",
    "company": None,
    "blog": "",
    "location": None,
    "email": None,
    "hireable": None,
    "bio": None,
    "twitter_username": None,
    "public_repos": 1,
    "public_gists": 1,
    "followers": 1,
    "following": 1,
    "created_at": "2000-01-01T00:00:00Z",
    "updated_at": "2021-01-01T00:00:00Z"
}

github_username_not_found_reply = {
    "message": "Not Found",
    "documentation_url": "https://docs.github.com/rest/reference/users#get-a-user"
}

github_email_search_reply = {
    "total_count": 1,
    "incomplete_results": False,
    "items": [
        {
            "login": "defunkt",
            "id": 42424242,
            "node_id": "MDQ6VXNlcjQyNDI0MjQy",
            "avatar_url": "https://avatars3.githubusercontent.com/u/42424242?v=4",
            "gravatar_id": "",
            "url": "https://api.github.com/users/defunkt",
            "html_url": "https://github.com/defunkt",
            "followers_url": "https://api.github.com/users/defunkt/followers",
            "following_url": "https://api.github.com/users/defunkt/following{/other_user}",
            "gists_url": "https://api.github.com/users/defunkt/gists{/gist_id}",
            "starred_url": "https://api.github.com/users/defunkt/starred{/owner}{/repo}",
            "subscriptions_url": "https://api.github.com/users/defunkt/subscriptions",
            "organizations_url": "https://api.github.com/users/defunkt/orgs",
            "repos_url": "https://api.github.com/users/defunkt/repos",
            "events_url": "https://api.github.com/users/defunkt/events{/privacy}",
            "received_events_url": "https://api.github.com/users/defunkt/received_events",
            "type": "User",
            "site_admin": False,
            "score": 1.0
        }
    ]
}

github_email_search_not_found_reply = {
    "total_count": 0,
    "incomplete_results": False,
    "items": [

    ]
}

github_commit_search_reply = {
    "total_count": 1,
    "incomplete_results": False,
    "items": [
        {
            "url": "https://api.github.com/repos/defunkt-org/defunkt-repo/"
                   "commits/1111111111111111111111111111111111111111",
            "sha": "1111111111111111111111111111111111111111",
            "node_id":
                "MDY6Q29tbWl0NDM0MzQzNDM6MTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTExMTEx",
            "html_url": "https://github.com/defunkt-org/defunkt-repo/"
                        "commit/1111111111111111111111111111111111111111",
            "comments_url": "https://api.github.com/repos/defunkt-org/defunkt-repo/"
                            "commits/1111111111111111111111111111111111111111/comments",
            "commit": {
                "url": "https://api.github.com/repos/defunkt-org/defunkt-repo/"
                       "git/commits/1111111111111111