Ejemplo n.º 1
0
    def test_api_is_validated_any_time_it_is_modified(self):
        self.client.force_login(self.user)
        # Setup a valid response
        valid_key = "valid_key"
        mock_user_info_response(valid_key)
        # Setup
        invalid_api_key = "invalid_key!"
        mock_invalid_api_user_info_response(invalid_api_key)

        # Test upon PUT in profile.
        self.user.profile.api_key = invalid_api_key
        self.user.profile.api_valid = False
        self.user.profile.save()

        response = self.client.put(reverse("api:profile-detail",
                                           args=(self.user.profile.id, )),
                                   data=self.user.profile.__dict__)
        self.assertEqual(response.status_code, 400)

        response = self.client.patch(reverse("api:profile-detail",
                                             args=(self.user.profile.id, )),
                                     data={"api_key": invalid_api_key})
        self.assertEqual(response.status_code, 400)

        # Make sure it doesnt get accidentally set if we patch another field.
        response = self.client.patch(reverse("api:profile-detail",
                                             args=(self.user.profile.id, )),
                                     data={"kanji_svg_draw_speed": 4})
        self.assertFalse(response.data['api_valid'])

        # Now patch it to a valid key, which should set it to true.
        response = self.client.patch(reverse("api:profile-detail",
                                             args=(self.user.profile.id, )),
                                     data={"api_key": valid_key})
        self.assertTrue(response.data['api_valid'])
Ejemplo n.º 2
0
    def test_when_user_resets_their_account_we_remove_all_reviews_and_then_unlock_their_current_level(self):
        self.user.profile.unlocked_levels.get_or_create(level=1)
        new_review = create_review(create_vocab("arbitrary word"), self.user)
        new_review.needs_review = True
        new_review.save()
        self.assertEqual(get_users_current_reviews(self.user).count(), 2)

        mock_vocab_list_response_with_single_vocabulary(self.user)
        mock_user_info_response(self.user.profile.api_key)

        reset_user(self.user, 1)

        self.user.refresh_from_db()
        self.user.profile.refresh_from_db()
        self.assertEqual(get_users_lessons(self.user).count(), 0)
        self.assertEqual(self.user.profile.level, 5)
Ejemplo n.º 3
0
    def test_adding_a_level_to_reset_command_only_resets_levels_above_or_equal_togiven(
            self):
        self.client.force_login(user=self.user)
        v = create_vocab("test")
        create_reading(v, "test", "test", 3)
        create_review(v, self.user)
        mock_user_info_response(self.user.profile.api_key)

        self.user.profile.unlocked_levels.get_or_create(level=2)
        response = self.client.get((reverse("api:review-current")))
        self.assertEqual(response.data['count'], 2)
        self.assertListEqual(self.user.profile.unlocked_levels_list(), [5, 2])
        self.client.post(reverse("api:user-reset"), data={'level': 3})

        response = self.client.get((reverse("api:review-current")))
        self.assertEqual(response.data['count'], 0)
        self.assertListEqual(self.user.profile.unlocked_levels_list(), [2])
Ejemplo n.º 4
0
    def test_vocabulary_meaning_changes_reflect_locally(self):
        self.client.force_login(self.user)

        self.assertEqual(self.vocabulary.meaning, "radioactive bat")

        mock_user_info_response(self.user.profile.api_key)
        mock_vocab_list_response_with_single_vocabulary_with_changed_meaning(self.user)

        sync_with_wk(self.user.id)

        self.vocabulary.refresh_from_db()
        self.assertEqual(
            self.vocabulary.meaning,
            sample_api_responses.single_vocab_response_with_changed_meaning[
                "requested_information"
            ][0]["meaning"],
        )
Ejemplo n.º 5
0
    def test_when_user_resets_their_account_we_remove_all_reviews_and_then_unlock_their_current_level(
        self
    ):
        self.user.profile.unlocked_levels.get_or_create(level=1)
        new_review = create_review(create_vocab("arbitrary word"), self.user)
        new_review.needs_review = True
        new_review.save()
        self.assertEqual(get_users_current_reviews(self.user).count(), 2)

        mock_vocab_list_response_with_single_vocabulary(self.user, self.user.profile.level)
        mock_user_info_response(self.user.profile.api_key)

        reset_user(self.user, 1)

        self.user.refresh_from_db()
        self.user.profile.refresh_from_db()
        self.assertEqual(get_users_lessons(self.user).count(), 0)
        self.assertEqual(self.user.profile.level, 5)
Ejemplo n.º 6
0
    def test_sending_put_to_profile_correctly_updates_information(self):
        self.client.force_login(self.user)
        mock_user_info_response(self.user.profile.api_key)

        response = self.client.get(reverse("api:profile-list"))
        data = response.data
        id = data["results"][0]["id"]
        request = data["results"][0]
        request["on_vacation"] = True
        response = self.client.put(
            reverse("api:profile-detail", args=(id,)), data=request, format="json"
        )
        assert response.status_code == 200
        self.user.refresh_from_db()
        self.user.profile.refresh_from_db()
        assert self.user.profile.vacation_date is not None
        data = response.data
        assert data is not None
Ejemplo n.º 7
0
    def test_sending_put_to_profile_correctly_updates_information(self):
        self.client.force_login(self.user)
        mock_user_info_response(self.user.profile.api_key)

        response = self.client.get(reverse("api:profile-list"))
        data = response.data
        id = data['results'][0]['id']
        request = data['results'][0]
        request['on_vacation'] = True
        response = self.client.put(reverse("api:profile-detail", args=(id, )),
                                   data=request,
                                   format='json')
        assert (response.status_code == 200)
        self.user.refresh_from_db()
        self.user.profile.refresh_from_db()
        assert (self.user.profile.vacation_date is not None)
        data = response.data
        assert (data is not None)
Ejemplo n.º 8
0
    def test_when_wanikani_changes_meaning_no_duplicate_is_created(self):
        resp_body = deepcopy(sample_api_responses.single_vocab_response)
        resp_body["requested_information"][0][
            "meaning"] = "NOT radioactive bat"

        # Mock response so that the level changes on our default vocab.
        mock_user_info_response(self.user.profile.api_key)
        responses.add(
            responses.GET,
            build_API_sync_string_for_user_for_levels(
                self.user, [self.user.profile.level]),
            json=resp_body,
            status=200,
            content_type="application/json",
        )

        Syncer.factory(self.user.profile).sync_with_wk()

        # Will fail if 2 vocab exist with same kanji.
        get_vocab_by_kanji("猫")
Ejemplo n.º 9
0
    def test_api_is_validated_any_time_it_is_modified(self):
        self.client.force_login(self.user)
        # Setup a valid response
        valid_key = "valid_key"
        mock_user_info_response(valid_key)
        # Setup
        invalid_api_key = "invalid_key!"
        mock_invalid_api_user_info_response(invalid_api_key)

        # Test upon PUT in profile.
        self.user.profile.api_key = invalid_api_key
        self.user.profile.api_valid = False
        self.user.profile.save()

        response = self.client.put(
            reverse("api:profile-detail", args=(self.user.profile.id,)),
            data=self.user.profile.__dict__,
        )
        self.assertEqual(response.status_code, 400)

        response = self.client.patch(
            reverse("api:profile-detail", args=(self.user.profile.id,)),
            data={"api_key": invalid_api_key},
        )
        self.assertEqual(response.status_code, 400)

        # Make sure it doesnt get accidentally set if we patch another field.
        response = self.client.patch(
            reverse("api:profile-detail", args=(self.user.profile.id,)),
            data={"kanji_svg_draw_speed": 4},
        )
        self.assertFalse(response.data["api_valid"])

        # Now patch it to a valid key, which should set it to true.
        response = self.client.patch(
            reverse("api:profile-detail", args=(self.user.profile.id,)),
            data={"api_key": valid_key},
        )
        self.assertTrue(response.data["api_valid"])
Ejemplo n.º 10
0
    def test_enable_follow_me_syncs_user_immediately(self):
        # Given
        mock_user_info_response(self.user.profile.api_key)
        mock_empty_vocabulary_response(
            self.user.profile.api_key, self.user.profile.level
        )
        self.client.force_login(self.user)
        self.user.profile.follow_me = False
        self.user.profile.save()
        response = self.client.get(reverse("api:profile-list"))
        data = response.data
        id = data["results"][0]["id"]

        # When
        self.client.patch(
            reverse("api:profile-detail", args=(id,)),
            data={"follow_me": True},
            format="json",
        )

        # Then
        self.user.refresh_from_db()
        self.user.profile.refresh_from_db()
        assert self.user.profile.follow_me is True