コード例 #1
0
    def test_newly_authenticated_user_is_authenticated(self):
        """
        This exists to catch an edge case where a user would be created but
        could not be authenticated against.

        Using User.objects.create(...) instead of User.objects.create_user(...)
        will not throw an error, but the created user can't be authenticated
        against.

        Who knows what Django does internally that's different from manually
        adding a user? Just don't delete this test.
        """
        email = "*****@*****.**"

        # Django does not include middleware in tests, so add it manually
        # The request can be nondescript because it's just to satisfy Django
        request = self.request_factory.get("/")
        SessionMiddleware().process_request(request)
        AuthenticationMiddleware().process_request(request)

        user = IdentityService.sign_up(request, email, "hunter2", "John",
                                       "Doe")

        self.assertTrue(user is not None)
        self.assertEqual(user.email, email)
コード例 #2
0
    def sign_up(self, request):
        """
        Create a new user and start an authenticated session with them
        """
        credentials = re.fullmatch(r"Basic (.*)",
                                   request.META["HTTP_AUTHORIZATION"])[1]
        email, password = base64.b64decode(
            credentials.encode()).decode().split(":")
        body = json.loads(request.body.decode("utf-8"))
        first_name = body["firstName"] if "firstName" in body else ""
        last_name = body["lastName"] if "lastName" in body else ""

        user = IdentityService.sign_up(request, email, password, first_name,
                                       last_name)
        return JsonResponse({"user": serialize_user(user)}, status=201)