Exemplo n.º 1
0
    def post(self, request, repository_name):
        access_token = take_access_token_from_session(request)
        if access_token:
            user = request.session['user']

            assert isinstance(user, GithubUser)

            resp_data = GithubAPI.get_user_public_repository(user.login, repository_name)
            github_repository = GithubRepositoryBuilder.build(resp_data, user)
            github_repository.save()

            # try to fetch repository by session_key and check if exists on file_system
            # before initializing the new one

            # also think of uniqueness of user_session

            session_key = request.session.session_key
            query_set = LocalRepository.objects.filter(user_session_key=session_key,
                                                       github_repository_id=github_repository.id) \
                .order_by('-last_modified')

            if query_set.exists():
                local_repo = query_set.first()
                if FileBrowser.repo_exists(local_repo.path):
                    return Response(data={'id': local_repo.id})

            local_repository = LocalRepositoryService.init_repository(session_key, github_repository)
            local_repository.save()

            return Response(data={'id': local_repository.id})
        else:
            return HttpResponseForbidden()
Exemplo n.º 2
0
    def test_get_user_public_repositories(self, github_api_requests_mock):
        access_token = "token"
        expected_repositories = [{"id": 1}, {"id": 2}]
        github_api_requests_mock.get.return_value = MockResponse(expected_repositories)

        repositories = GithubAPI.get_user_public_repositories(access_token)

        self.assertEqual(repositories, expected_repositories)
        github_api_requests_mock.get.assert_called_once_with("/user/repos?type=public", access_token)
Exemplo n.º 3
0
    def test_get_user(self, github_api_requests_mock):
        access_token = "token"
        expected_user = "******"
        github_api_requests_mock.get.return_value = MockResponse(expected_user)

        user = GithubAPI.get_user(access_token)

        self.assertEqual(user, expected_user)
        github_api_requests_mock.get.assert_called_once_with("user", access_token)
Exemplo n.º 4
0
    def get(self, request):
        access_token = take_access_token_from_session(request)

        if access_token:
            resp_data = GithubAPI.get_user_public_repositories(access_token)
            serialize = lambda x: take_keys(x, ['id', 'name', 'full_name', 'clone_url', 'description'])
            return Response(data=map(serialize, resp_data))
        else:
            return HttpResponseForbidden()
Exemplo n.º 5
0
    def get(self, request):

        if 'code' in request.query_params:
            code = request.query_params['code']
            access_token = GithubAPI.get_access_token(CLIENT_ID, SECRET_KEY, code)

            resp_data = GithubAPI.get_user(access_token)
            print(resp_data)

            user = GithubUserBuilder.build(resp_data)
            user.save()

            request.session['access_token'] = access_token
            request.session['user'] = user

            return redirect('/')
        else:
            raise Exception()
Exemplo n.º 6
0
    def test_get_access_token(self, mock_requests):
        client_id = "client_id"
        client_secret = "client_secret"
        code = "code"
        expected_token = "access_token"
        mock_requests.post.return_value = MockResponse({"access_token": expected_token})

        actual_token = GithubAPI.get_access_token(client_id, client_secret, code)

        mock_requests.post.assert_called_once_with(
            "url/access_token",
            {"client_secret": client_secret, "code": code, "client_id": client_id},
            headers={"accept": "application/json"},
        )

        self.assertEqual(actual_token, expected_token)