コード例 #1
0
 def test_submission_create_with_full_args(self, client: Client) -> None:
     """Test whether creation with all arguments is successful."""
     client, headers, _ = setup_user_client(client)
     source = get_default_test_source()
     data = {
         "original_id": "spaaaaace",
         "source": source.pk,
         "url": "http://example.com",
         "tor_url": "http://example.com/tor",
         "content_url": "http://a.com",
         "title": "This is a Submission",
         "nsfw": False,
     }
     result = client.post(
         reverse("submission-list"),
         data,
         content_type="application/json",
         **headers,
     )
     assert result.status_code == status.HTTP_201_CREATED
     submission = Submission.objects.get(id=result.json()["id"])
     assert submission.original_id == data["original_id"]
     assert submission.source == source
     assert submission.url == data["url"]
     assert submission.tor_url == data["tor_url"]
     assert submission.content_url == data["content_url"]
     assert submission.nsfw == data["nsfw"]
     assert submission.title == data["title"]
コード例 #2
0
    def test_failed_ocr_on_create(self, client: Client,
                                  settings: SettingsWrapper) -> None:
        """Verify that a new submission completes the OCR process."""
        settings.ENABLE_OCR = True
        settings.IMAGE_DOMAINS = ["example.com"]
        assert Transcription.objects.count() == 0

        client, headers, _ = setup_user_client(client)
        source = get_default_test_source()
        data = {
            "original_id": "spaaaaace",
            "source": source.pk,
            "content_url": "http://example.com/a.jpg",
        }

        with patch("api.models.process_image", return_value=None) as mock:
            result = client.post(
                reverse("submission-list"),
                data,
                content_type="application/json",
                **headers,
            )
            mock.assert_called_once()

        assert result.status_code == status.HTTP_201_CREATED
        assert Transcription.objects.count() == 0
        assert result.json().get("cannot_ocr") is True
コード例 #3
0
    def test_ocr_on_create_with_cannot_ocr_flag(
            self, client: Client, settings: SettingsWrapper) -> None:
        """Verify the OCR process exits early if the cannot_ocr flag is already set."""
        settings.ENABLE_OCR = True
        settings.IMAGE_DOMAINS = ["example.com"]
        assert Transcription.objects.count() == 0

        client, headers, _ = setup_user_client(client)
        source = get_default_test_source()
        data = {
            "original_id": "spaaaaace",
            "source": source.pk,
            "content_url": "http://example.com/a.jpg",
            "cannot_ocr": "True",
        }

        with patch("api.models.process_image", return_value={"text":
                                                             "AAA"}) as mock:
            # mock it anyway just in case this fails -- we don't want to actually
            # call OCR
            result = client.post(
                reverse("submission-list"),
                data,
                content_type="application/json",
                **headers,
            )
            mock.assert_not_called()

        assert result.status_code == status.HTTP_201_CREATED
        assert Transcription.objects.count() == 0
コード例 #4
0
    def test_create_no_id(self, client: Client) -> None:
        """Test whether a request without submission ID is considered a bad request."""
        client, headers, _ = setup_user_client(client)
        source = get_default_test_source()
        data = {"source": source.pk}

        result = client.post(
            reverse("submission-list"),
            json.dumps(data),
            content_type="application/json",
            **headers,
        )

        assert result.status_code == status.HTTP_400_BAD_REQUEST
コード例 #5
0
 def test_create_invalid_submission_id(self, client: Client) -> None:
     """Test whether a creation with an invalid submission ID is caught correctly."""
     client, headers, user = setup_user_client(client)
     source = get_default_test_source()
     data = {
         "submission_id": 404,
         "original_id": "base_id",
         "username": user.username,
         "source": source.name,
         "url": "https://example.com",
         "text": "test content",
     }
     result = client.post(
         reverse("transcription-list"),
         json.dumps(data),
         content_type="application/json",
         **headers,
     )
     assert result.status_code == status.HTTP_404_NOT_FOUND
コード例 #6
0
ファイル: test_source.py プロジェクト: GrafeasGroup/blossom
    def test_list(self, client: Client) -> None:
        """Verify that listing all Source objects works correctly."""
        Source.objects.all().delete()  # clear out system ones for test
        client, headers, _ = setup_user_client(client)
        result = client.get(reverse("source-list"),
                            content_type="application/json",
                            **headers)

        assert result.status_code == status.HTTP_200_OK
        assert result.json()["count"] == 0

        source = get_default_test_source()

        result = client.get(reverse("source-list"),
                            content_type="application/json",
                            **headers)

        assert result.status_code == status.HTTP_200_OK
        assert result.json()["count"] == 1
        assert result.json()["results"][0]["name"] == source.name
コード例 #7
0
 def test_create_minimum_args(self, client: Client) -> None:
     """Test whether creation with minimum arguments is successful."""
     client, headers, _ = setup_user_client(client)
     source = get_default_test_source()
     data = {
         "original_id": "spaaaaace",
         "source": source.pk,
         "content_url": "https://a.com",
     }
     result = client.post(
         reverse("submission-list"),
         data,
         content_type="application/json",
         **headers,
     )
     assert result.status_code == status.HTTP_201_CREATED
     submission = Submission.objects.get(id=result.json()["id"])
     assert submission.original_id == data["original_id"]
     assert submission.source == source
     assert submission.content_url == data["content_url"]