コード例 #1
0
ファイル: tests.py プロジェクト: alekseystryukov/treenet
    def test_auth_get_with_token(self):
        self.assertIsNone(UserProfile.objects.first())
        user = UserProfile.create_from_google_info(self.user_info)

        extra = {'HTTP_AUTHORIZATION': 'Token {}'.format(user.token)}
        response = self.client.get(self.base_url, **extra)

        self.assertEqual(response.status_code, 200)
        user = response.data
        self.assertIsNotNone(user)
        self.assertEqual(user["name"], self.user_info["name"])
        self.assertEqual(user["picture"], self.user_info["picture"])
コード例 #2
0
ファイル: tests.py プロジェクト: alekseystryukov/treenet
    def test_auth_patch(self):
        self.assertIsNone(UserProfile.objects.first())
        user = UserProfile.create_from_google_info(self.user_info)

        extra = {
            'HTTP_AUTHORIZATION': 'Token {}'.format(user.token),
            'CONTENT_TYPE': 'application/json',
        }
        update_data = dict(name="John Kim Chen")
        response = self.client.patch(
            self.base_url,
            json.dumps(update_data),
            **extra
        )
        self.assertEqual(response.status_code, 200)
        user = response.data
        self.assertEqual(user["name"], update_data["name"])
コード例 #3
0
    def get(request):

        if isinstance(request.user, UserProfile):
            return Response(data=UserSerializer(request.user).data)

        # 2nd step of Oauth
        if 'code' in request.GET:
            code = request.GET.get('code')
            user_info = get_user_info(code)
            try:
                user = UserProfile.objects.get(google_email=user_info["email"])
            except UserProfile.DoesNotExist:
                user = UserProfile.create_from_google_info(user_info)
            else:
                user.update_with_google_info(user_info)
            return Response(data=UserSerializer(user).data)

        return Response({"client_id": GOOGLE_CLIENT_ID},
                        status=status.HTTP_403_FORBIDDEN)