Example #1
0
    def test_options(self):
        user = create_user()

        login_user(self.client, user)
        response = self.client.options(self.base_url)

        self.assert_status_equal(response, status.HTTP_200_OK)
Example #2
0
    def test_post_not_set_new_password_if_wrong_current_password(self):
        user = create_user()
        data = {"new_password": "******", "current_password": "******"}
        login_user(self.client, user)

        response = self.client.post(self.base_url, data)

        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
Example #3
0
    def test_post_not_set_new_username_if_same(self):
        user = create_user()
        data = {"new_username": "******", "current_password": "******"}
        login_user(self.client, user)

        response = self.client.post(self.base_url, data, user=user)

        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
        self.assertTrue(user.is_active)
Example #4
0
    def test_post_update_username_and_send_activation_email(self):
        user = create_user()
        data = {"new_username": "******", "current_password": "******"}
        login_user(self.client, user)

        response = self.client.post(self.base_url, data, user=user)

        self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
        self.assert_emails_in_mailbox(1)
        self.assert_email_exists(to=[user.email])
Example #5
0
    def test_none_token_model_results_in_no_operation(self):
        user = create_user()
        user_logged_out.connect(self.signal_receiver)

        login_user(self.client, user)
        response = self.client.post(self.base_url)

        self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
        self.assertEqual(response.data, None)
        self.assertFalse(self.signal_sent)
Example #6
0
    def test_post_should_logout_logged_in_user(self):
        user = create_user()
        user_logged_out.connect(self.signal_receiver)

        login_user(self.client, user)
        response = self.client.post(self.base_url)

        self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
        self.assertEqual(response.data, None)
        self.assertTrue(self.signal_sent)
Example #7
0
    def test_not_delete_if_fails_password_validation(self):
        user = create_user()
        self.assert_instance_exists(User, username="******")
        data = {"current_password": "******"}
        login_user(self.client, user)

        response = self.client.delete(self.base_url, data=data)

        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data, {"current_password": ["Invalid password."]})
Example #8
0
    def test_post_not_set_new_username_if_invalid(self):
        user = create_user()
        data = {"new_username": "******", "current_password": "******"}
        login_user(self.client, user)

        response = self.client.post(self.base_url, data, user=user)

        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
        user.refresh_from_db()
        self.assertNotEqual(user.username, data["new_username"])
Example #9
0
    def test_post_logout_after_password_change(self):
        user = create_user()
        data = {"new_password": "******", "current_password": "******"}
        login_user(self.client, user)

        response = self.client.post(self.base_url, data)

        self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
        is_logged = Token.objects.filter(user=user).exists()
        self.assertFalse(is_logged)
Example #10
0
    def test_delete_user_if_logged_in(self):
        user = create_user()
        self.assert_instance_exists(User, username="******")
        data = {"current_password": "******"}
        login_user(self.client, user)

        response = self.client.delete(self.base_url, data=data, user=user)

        self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
        self.assert_instance_does_not_exist(User, username="******")
Example #11
0
    def test_post_set_new_username(self):
        user = create_user()
        data = {"new_username": "******", "current_password": "******"}
        login_user(self.client, user)

        response = self.client.post(self.base_url, data, user=user)

        self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
        user.refresh_from_db()
        self.assertEqual(data["new_username"], user.username)
Example #12
0
    def test_post_not_set_new_username_if_wrong_current_password(self):
        user = create_user()
        orig_username = user.get_username()
        data = {"new_username": "******", "current_password": "******"}
        login_user(self.client, user)

        response = self.client.post(self.base_url, data, user=user)

        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
        user.refresh_from_db()
        self.assertEqual(orig_username, user.username)
Example #13
0
    def test_post_set_new_password(self):
        user = create_user()
        data = {"new_password": "******", "current_password": "******"}
        login_user(self.client, user)

        response = self.client.post(self.base_url, data)

        self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
        user.refresh_from_db()
        self.assertTrue(user.check_password(data["new_password"]))
        self.assert_emails_in_mailbox(0)
Example #14
0
    def test_post_not_set_new_username_if_exists(self):
        username = "******"
        create_user(username=username)
        user = create_user(username="******")
        data = {"new_username": username, "current_password": "******"}
        login_user(self.client, user)

        response = self.client.post(self.base_url, data, user=user)

        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
        user.refresh_from_db()
        self.assertNotEqual(user.username, username)
Example #15
0
    def test_post_password_changed_confirmation_email(self):
        user = create_user()
        data = {"new_password": "******", "current_password": "******"}
        login_user(self.client, user)

        response = self.client.post(self.base_url, data, user=user)

        self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)
        user.refresh_from_db()
        self.assertTrue(user.check_password(data["new_password"]))
        self.assert_emails_in_mailbox(1)
        self.assert_email_exists(to=[user.email])
Example #16
0
    def test_patch_edits_user_attribute(self):
        user = create_user()
        login_user(self.client, user)
        response = self.client.patch(
            path=reverse("user-detail", args=(user.pk,)),
            data={"email": "*****@*****.**"},
        )

        self.assert_status_equal(response, status.HTTP_200_OK)
        self.assertTrue("email" in response.data)

        user.refresh_from_db()
        self.assertTrue(user.email == "*****@*****.**")
Example #17
0
    def test_post_not_set_new_password_if_mismatch(self):
        user = create_user()
        data = {
            "new_password": "******",
            "re_new_password": "******",
            "current_password": "******",
        }
        login_user(self.client, user)

        response = self.client.post(self.base_url, data)

        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
        user.refresh_from_db()
        self.assertTrue(user.check_password(data["current_password"]))
Example #18
0
    def test_post_not_set_new_password_if_fails_validation(self):
        user = create_user()
        data = {
            "new_password": "******",
            "re_new_password": "******",
            "current_password": "******",
        }
        login_user(self.client, user)

        response = self.client.post(self.base_url, data)

        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data,
                         {"new_password": ["Password 666 is not allowed."]})
Example #19
0
    def test_post_not_set_new_username_if_mismatch(self):
        user = create_user()
        data = {
            "new_username": "******",
            "re_new_username": "******",
            "current_password": "******",
        }
        login_user(self.client, user)

        response = self.client.post(self.base_url, data, user=user)

        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
        user.refresh_from_db()
        self.assertNotEqual(data["new_username"], user.username)
Example #20
0
    def test_patch_cant_edit_others_attribute(self):
        user = create_user()
        another_user = create_user(
            **{"username": "******", "password": "******", "email": "*****@*****.**"}
        )
        login_user(self.client, user)
        response = self.client.patch(
            path=reverse("user-detail", args=(another_user.pk,)),
            data={"email": "*****@*****.**"},
        )

        self.assert_status_equal(response, status.HTTP_404_NOT_FOUND)

        another_user.refresh_from_db()
        self.assertTrue(another_user.email == "*****@*****.**")
Example #21
0
    def test_post_logout_cycle_session(self):
        user = create_user()
        data = {"new_password": "******", "current_password": "******"}
        login_user(self.client, user)
        self.client.force_login(user)

        response = self.client.post(self.base_url, data)
        self.assert_status_equal(response, status.HTTP_204_NO_CONTENT)

        user.refresh_from_db()

        session_id = self.client.cookies["sessionid"].coded_value
        engine = importlib.import_module(settings.SESSION_ENGINE)
        session = engine.SessionStore(session_id)
        session_key = session[HASH_SESSION_KEY]

        self.assertEqual(session_key, user.get_session_auth_hash())
Example #22
0
    def test_put_edits_user_attribute(self):
        user_data = {
            "username": "******",
            "password": "******",
            "email": "*****@*****.**",
        }
        user = create_user(**user_data)
        user_data["password"] = "******"
        login_user(self.client, user)

        response = self.client.patch(
            path=reverse("user-detail", args=(user.pk,)), data=user_data
        )

        self.assert_status_equal(response, status.HTTP_200_OK)

        user.refresh_from_db()
        self.assertTrue(user.email == "*****@*****.**")
Example #23
0
    def test_put_cant_edit_others_attribute(self):
        user = create_user()
        another_user_data = {
            "username": "******",
            "password": "******",
            "email": "*****@*****.**",
        }
        another_user = create_user(**another_user_data)
        another_user_data["password"] = "******"
        another_user_data["email"] = "*****@*****.**"
        login_user(self.client, user)

        response = self.client.patch(
            path=reverse("user-detail", args=(another_user.pk,)), data=another_user_data
        )

        self.assert_status_equal(response, status.HTTP_404_NOT_FOUND)

        another_user.refresh_from_db()
        assert another_user.email, "*****@*****.**"
Example #24
0
 def setUp(self):
     self.base_url = reverse("user-me")
     self.user = create_user()
     login_user(self.client, self.user)
Example #25
0
    def test_user_cannot_list_other_users(self):
        login_user(self.client, self.user)
        response = self.client.get(self.base_url)

        self.assert_status_equal(response, status.HTTP_200_OK)
        self.assertEqual(len(response.json()), 1)
Example #26
0
    def test_superuser_can_list_all_users(self):
        login_user(self.client, self.superuser)
        response = self.client.get(self.base_url)

        self.assert_status_equal(response, status.HTTP_200_OK)
        self.assertEqual(len(response.json()), 2)
Example #27
0
    def test_superuser_can_get_other_user_detail(self):
        login_user(self.client, self.superuser)
        response = self.client.get(reverse("user-detail", args=[self.user.pk]))

        self.assert_status_equal(response, status.HTTP_200_OK)