Пример #1
0
    def test_list_view_returns_sorted_correctly(self):
        # should return greatest to smallest, that way easier to truncate
        WriteUpPromptFactory.create_batch(
            100, share_state=PromptShareStates.PUBLISHED)
        url = reverse(self.VIEW_NAME)
        response = self.registered_user_client.get(url)
        data = response.data

        score = [item["score"] for item in data]

        sorted_score = sorted(score, reverse=True)
        self.assertEqual(score, sorted_score)
Пример #2
0
    def test_view(self):
        prompt = WriteUpPromptFactory()
        prompt_uuid = prompt.uuid_str
        url_kwargs = {"prompt_uuid": prompt_uuid}
        url = reverse(self.VIEW_NAME, kwargs=url_kwargs)

        original_prompt_score = prompt.score
        data = {"value": 1}

        response = self.registered_user_client.post(url, data=data)
        self.assertEqual(response.status_code, 200)

        prompt.refresh_from_db()
        # the score counter isn't fully real-time, it's like meh
        self.assertTrue(prompt.score > original_prompt_score)
Пример #3
0
    def test_access_permission_for_not_owner(self):
        user = UserFactory()

        diff_user_prompt = WriteUpPromptFactory()
        valid = user_can_read_prompt_instance(user, diff_user_prompt)

        self.assertFalse(valid)
Пример #4
0
    def test_view(self):
        prompt = WriteUpPromptFactory()
        data_kwargs = {"prompt_uuid": prompt.uuid_str}
        url = reverse(self.VIEW_NAME, kwargs=data_kwargs)

        response = self.registered_user_client.post(url)
        self.assertEqual(response.status_code, 200)
Пример #5
0
    def test_get_view_no_permission(self):
        mock_title = "I Want Noodles"
        prompt = WriteUpPromptFactory(title=mock_title)
        data_kwargs = {"prompt_uuid": prompt.uuid_str}
        url = reverse(self.VIEW_NAME, kwargs=data_kwargs)

        response = self.registered_user_client.get(url)
        self.assertEqual(response.status_code, 404)
Пример #6
0
    def test_list_view_wont_return_bad_stuff(self):
        WriteUpPromptFactory(
            staff_verified_share_state=StaffVerifiedShareStates.VERIFIED_FAIL)
        url = reverse(self.VIEW_NAME)

        response = self.registered_user_client.get(url)

        self.assertEqual(0, len(response.data))
Пример #7
0
    def test_view_updates_score(self):
        prompt_uuid = WriteUpPromptFactory().uuid_str
        url_kwargs = {"prompt_uuid": prompt_uuid}
        url = reverse(self.VIEW_NAME, kwargs=url_kwargs)

        data = {"value": 3}

        response = self.registered_user_client.post(url, data=data)
        self.assertEqual(response.status_code, 200)
Пример #8
0
    def test_no_login_has_no_access(self):
        prompt_uuid = WriteUpPromptFactory().uuid_str
        url_kwargs = {"prompt_uuid": prompt_uuid}
        url = reverse(self.VIEW_NAME, kwargs=url_kwargs)

        data = {"value": 3}

        response = self.unregistered_user_client.post(url, data=data)
        self.assertEqual(response.status_code, 401)
Пример #9
0
    def test_view_delete(self):
        prompt = WriteUpPromptFactory()
        WriteUpFlaggedPromptFactory(user=self.registered_user, prompt=prompt)

        data_kwargs = {"prompt_uuid": prompt.uuid_str}
        url = reverse(self.VIEW_NAME, kwargs=data_kwargs)

        response = self.registered_user_client.delete(url)
        self.assertEqual(response.status_code, 204)
Пример #10
0
    def test_delete_view_wont_allow_for_not_owner(self):
        # prompt = WriteUpPromptFactory(user=self.registered_user)
        prompt = WriteUpPromptFactory()
        data_kwargs = {"prompt_uuid": prompt.uuid_str}
        url = reverse(self.VIEW_NAME, kwargs=data_kwargs)

        client = self.registered_user_client
        response = client.delete(url)
        self.assertEqual(response.status_code, 404)
Пример #11
0
    def test_access_permission_for_not_owner_failed(self):
        user = UserFactory()

        diff_user_prompt = WriteUpPromptFactory(
            share_state=PromptShareStates.PUBLISHED,
            staff_verified_share_state=StaffVerifiedShareStates.VERIFIED_FAIL,
        )
        valid = user_can_read_prompt_instance(user, diff_user_prompt)

        self.assertFalse(valid)
Пример #12
0
    def test_post_view_multiple_times_only_results_in_one(self):
        prompt = WriteUpPromptFactory()
        data_kwargs = {"prompt_uuid": prompt.uuid_str}
        url = reverse(self.VIEW_NAME, kwargs=data_kwargs)

        for _ in range(3):
            self.registered_user_client.post(url)

        instance_count = WriteUpFlaggedPrompt.objects.filter(
            user=self.registered_user, prompt=prompt).count()
        self.assertEqual(instance_count, 1)
Пример #13
0
    def test_delete_view(self):
        prompt = WriteUpPromptFactory(user=self.registered_user)
        data_kwargs = {"prompt_uuid": prompt.uuid_str}
        url = reverse(self.VIEW_NAME, kwargs=data_kwargs)

        client = self.registered_user_client
        response = client.delete(url)
        self.assertEqual(response.status_code, 204)

        exists = WriteUpPrompt.objects.filter(uuid=prompt.uuid_str).exists()
        self.assertFalse(exists)
Пример #14
0
    def test_get_view_marked_as_failed_flagged_cannot_be_accessed(self):
        mock_title = "I Want Noodles"
        prompt = WriteUpPromptFactory(
            title=mock_title,
            share_state=PromptShareStates.PUBLISHED,
            staff_verified_share_state=StaffVerifiedShareStates.VERIFIED_FAIL,
        )
        data_kwargs = {"prompt_uuid": prompt.uuid_str}
        url = reverse(self.VIEW_NAME, kwargs=data_kwargs)

        response = self.registered_user_client.get(url)
        self.assertEqual(response.status_code, 404)
Пример #15
0
    def test_writeup_prompt_factory_loaded(self):
        text = "Jeff Was Here"
        email = "Jeff Was Here"
        title = "Jeff Was Here"

        writeup_prompt = WriteUpPromptFactory(text=text,
                                              email=email,
                                              title=title)

        self.assertEqual(writeup_prompt.text, text)
        self.assertEqual(writeup_prompt.email, text)
        self.assertEqual(writeup_prompt.title, text)
Пример #16
0
    def test_get_view(self):
        mock_title = "I Want Noodles"
        prompt = WriteUpPromptFactory(title=mock_title,
                                      user=self.registered_user)
        data_kwargs = {"prompt_uuid": prompt.uuid_str}
        url = reverse(self.VIEW_NAME, kwargs=data_kwargs)

        response = self.registered_user_client.get(url)
        self.assertEqual(response.status_code, 200)

        data = response.data
        self.assertEqual(data["uuid"], prompt.uuid_str)
        self.assertEqual(data["title"], prompt.title)
Пример #17
0
    def test_get_view_marked_as_public_can_be_accessed(self):
        mock_title = "I Want Noodles"
        prompt = WriteUpPromptFactory(title=mock_title,
                                      share_state=PromptShareStates.PUBLISHED)
        data_kwargs = {"prompt_uuid": prompt.uuid_str}
        url = reverse(self.VIEW_NAME, kwargs=data_kwargs)

        response = self.registered_user_client.get(url)
        self.assertEqual(response.status_code, 200)

        data = response.data
        self.assertEqual(data["uuid"], prompt.uuid_str)
        self.assertEqual(data["title"], prompt.title)
Пример #18
0
    def test_post_multiple_times_only_results_in_one_record(self):
        prompt_uuid = WriteUpPromptFactory().uuid_str
        url_kwargs = {"prompt_uuid": prompt_uuid}
        url = reverse(self.VIEW_NAME, kwargs=url_kwargs)

        data = {"value": 3}

        for _ in range(3):
            self.registered_user_client.post(url, data=data)

        count = WriteUpPromptVote.objects.filter(
            user=self.registered_user, prompt__uuid=prompt_uuid).count()
        self.assertEqual(count, 1)
Пример #19
0
    def test_view_with_api_client(self):
        """ Having some odd issues with auth - figure out why"""
        prompt_uuid = WriteUpPromptFactory().uuid_str
        url_kwargs = {"prompt_uuid": prompt_uuid}
        url = reverse(self.VIEW_NAME, kwargs=url_kwargs)

        valid_token = self.registered_user.auth_token.key

        client = APIClient()
        client.credentials(HTTP_AUTHORIZATION="Token " + valid_token)

        data = {"value": 3}
        response = client.post(url, data=data)
        self.assertEqual(response.status_code, 200)
Пример #20
0
    def test_list_view_query_count(self):
        WriteUpPromptFactory.create_batch(
            100, share_state=PromptShareStates.PUBLISHED)
        WriteUpPromptFactory.create_batch(
            5, share_state=PromptShareStates.PUBLISHED_LINK_ACCESS_ONLY)
        WriteUpPromptFactory.create_batch(
            5, share_state=PromptShareStates.UNSHARED)
        url = reverse(self.VIEW_NAME)

        # it's currently five queries for the view / auth login / some permission checking
        with self.assertNumQueriesLessThan(6):
            response = self.registered_user_client.get(url)
            self.assertEqual(response.status_code, 200)
Пример #21
0
    def test_list_view_returns_published_stuff(self):
        WriteUpPromptFactory.create_batch(
            5, share_state=PromptShareStates.PUBLISHED)
        WriteUpPromptFactory.create_batch(
            5, share_state=PromptShareStates.PUBLISHED_LINK_ACCESS_ONLY)
        WriteUpPromptFactory.create_batch(
            5, share_state=PromptShareStates.UNSHARED)

        url = reverse(self.VIEW_NAME)
        response = self.registered_user_client.get(url)
        data = response.data

        # should only see 5, the ones that are PUBLISHED
        self.assertEqual(5, len(response.data))

        # we synthetically generate score on prompts
        result = data[0]
        self.assertIsNotNone(result["score"])
Пример #22
0
    def test_access_permission_for_user_owner(self):
        user = UserFactory()
        user_prompt = WriteUpPromptFactory(user=user)
        valid = user_can_read_prompt_instance(user, user_prompt)

        self.assertTrue(valid)