コード例 #1
0
    def test_API_user(self):
        # client = RequestsClient()
        #
        # # Obtain a CSRF token.
        # response = client.get('http://127.0.0.1:9090/api-auth/login/')
        # assert response.status_code == 200
        # csrftoken = response.cookies['csrftoken']
        #
        # # Interact with the API.
        # response = client.post('http://127.0.0.1:9090/api-auth/login/', json={
        #     'username': '******',
        #     'password': '******'
        # }, headers={'X-CSRFToken': csrftoken})
        # assert response.status_code == 200

        from requests.auth import HTTPBasicAuth

        # Include an appropriate `Authorization:` header on all requests.
        # token = Token.objects.get(user__username='******')
        # client = APIClient()
        # client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)

        client = RequestsClient()
        client.auth = HTTPBasicAuth('jody', 'rekless')
        # client.headers.update({'x-test': 'true'})
        test = client.login()

        response = client.post('http://127.0.0.1:9090/api-auth/login/')
        assert response.status_code == 200
コード例 #2
0
 def test_get_docs(self):
     client = RequestsClient()
     client.auth = HTTPBasicAuth(self.username, self.password)
     client.headers.update({'x-test': 'true'})
     res = client.get('http://localhost:8000/docs/')
     self.assertEqual(res.status_code, 200)
     self.assertEqual(res.headers['Content-Type'],
                      "text/html; charset=utf-8")
コード例 #3
0
 def test_get_schema(self):
     client = RequestsClient()
     client.auth = HTTPBasicAuth(self.username, self.password)
     client.headers.update({'x-test': 'true'})
     res = client.get('http://localhost:8000/schema/')
     self.assertEqual(res.status_code, 200)
     self.assertEqual(res.headers['Content-Type'],
                      "application/coreapi+json")
コード例 #4
0
 def test_get_all_wishes(self):
     client = RequestsClient()
     client.auth = HTTPBasicAuth(self.username, self.password)
     client.headers.update({'x-test': 'true'})
     for ro in self.test_1:
         row = json.loads(ro)
         res = {}
         if row['request']['method'] == "GET":
             res = client.get('http://localhost:8000' +
                              row['request']['url'] + '/')
         elif row['request']['method'] == "POST":
             res = client.post('http://localhost:8000' +
                               row['request']['url'] + '/',
                               json=row['request']['body'])
         self.assertEqual(res.status_code, row['response']['status_code'])
         if row['response']['headers'] != {}:
             self.assertEqual(res.headers['Content-Type'],
                              row['response']['headers']['Content-Type'])
         if row['response']['body'] != {}:
             response = json.loads(res.text)
             self.assertEqual(response, row['response']['body'])
コード例 #5
0
def test_integration_workflow(docapi_env):
    u1 = UserFactory()

    # request-related things
    factory = APIRequestFactory()
    c = RequestsClient()
    c.auth = HTTPBasicAuth(u1.username, "password")

    assert Document.objects.count() == 0
    assert DocumentFile.objects.count() == 0
    certificate_body = CERT_EXAMPLE.copy()

    # create some certificate
    resp = c.post(
        "http://testserver/api/documents/v0/CertificatesOfOrigin/",
        json=certificate_body,
    )
    assert resp.status_code == 201, resp.content
    assert "id" in resp.json()
    cert_id = resp.json().get("id")

    cert = Document.objects.get(pk=cert_id)
    assert Document.objects.count() == 1
    assert DocumentFile.objects.count(
    ) == 0  # because there is no file info in the initial payload
    assert cert.document_number == certificate_body["certificateOfOrigin"][
        "id"]

    # retrieve it
    request = factory.get(
        f"api/documents/v0/CertificatesOfOrigin/{cert.pk}/",
        {
            "certificateOfOrigin": {
                "freeTradeAgreement": "CHAFTA",
                "name": "lalala"
            }
        },
        format="json",
    )
    force_authenticate(request, user=u1)
    resp = CertificateViewSet.as_view({"get": "retrieve"})(request, pk=cert.pk)
    assert resp.status_code == 200, resp.data
    cert_data = resp.data["certificateOfOrigin"]
    assert resp.data["id"] == cert.pk
    assert cert_data[
        "freeTradeAgreement"] == "China-Australia Free Trade Agreement"
    cert.refresh_from_db()
    assert cert.fta and cert.fta.name == "China-Australia Free Trade Agreement"

    # update it
    request = factory.patch(
        f"api/documents/v0/CertificatesOfOrigin/{cert.pk}/",
        {
            "certificateOfOrigin": {
                "freeTradeAgreement": "AANZFTA First Protocol",
                "name": "lalala",
            }
        },
        format="json",
    )
    force_authenticate(request, user=u1)
    resp = CertificateViewSet.as_view({"patch": "update"})(request, pk=cert.pk)
    assert resp.status_code == 200, resp.data
    cert.refresh_from_db()
    cert_data = cert.raw_certificate_data["certificateOfOrigin"]
    # just ensure that we didn't overwrite all the data
    assert len(cert_data.keys()) > 5, cert_data
    assert cert_data["freeTradeAgreement"] == "AANZFTA First Protocol"
    assert cert.fta and cert.fta.name == "AANZFTA First Protocol"
    # TODO: file upload
    # TODO: change status
    return
コード例 #6
0
ファイル: test_certs.py プロジェクト: roleyfoley/chambers-app
def test_integration_workflow(mock_post_message, mock_post_document):
    """
    Primitive but correct integration test, which creates certificate, uploads
    some dumb documents and marks it as "lodged".
    Doesn't check error-handling code branches (incorrect input, auth and so on),
    just displays that the workflow works fine in general.
    More specific unit tests are expected to test error handling and complicated cases
    (like document removed from the certificate and the certificate is not ready for
    lodging anymore).
    """
    mock_post_document.return_value = {
        "multihash": "QmNTWr4pXQcFd49PwgLBAPGjaedLkLXQU1c2EqfKV3K8RJ"
    }

    u1 = UserFactory()
    org = OrgFactory()

    c = RequestsClient()
    c.auth = HTTPBasicAuth(u1.username, 'password')

    message_body = {
        "org":
        str(org.pk),
        "dst_country":
        "CN",
        "exporter_info":
        "value",
        "producer_info":
        "value 02",
        "importer_info":
        "value 03",
        "goods_descr":
        "value of the goods description, which may be quite long or contain newlines",
    }

    mock_post_message.return_value = message_body.copy()

    # create some certificate
    resp = c.post('http://testserver/api/certificate/v0/certificate/',
                  data=message_body)
    assert resp.status_code == 201
    assert 'id' in resp.json()
    cert_id = resp.json().get('id')

    cert = Certificate.objects.get(org=org, pk=cert_id)

    assert cert.dst_country == 'CN'

    # upload some file
    cert.refresh_from_db()

    assert cert.status == cert.STATUS_DRAFT

    for file_type in (CertificateDocument.TYPE_INFORMATION_FORM,
                      CertificateDocument.TYPE_EVIDENCE_OF_ORIGIN):
        factory = APIRequestFactory()
        data = {'type': file_type, 'file': open('pytest.ini')}
        content = encode_multipart('BoUnDaRyStRiNg', data)
        content_type = 'multipart/form-data; boundary=BoUnDaRyStRiNg'
        request = factory.post(
            f'http://testserver/api/certificate/v0/certificate/{cert_id}/document/',
            content,
            content_type=content_type)
        request.user = u1
        resp = DocumentUploadView.as_view()(request, pk=cert.pk)

        print(resp)
        assert resp.status_code == 201

    cert.refresh_from_db()
    assert cert.status == cert.STATUS_COMPLETE

    # mark as lodged

    resp = c.patch(
        f'http://testserver/api/certificate/v0/certificate/{cert_id}/status/',
        data={"status": "lodged"})
    assert resp.status_code == 200

    cert.refresh_from_db()
    assert cert.status == cert.STATUS_SENT