Pand-Aid / pandaid-api

backend for Pand-Aid pandemic response app
3 stars 3 forks source link

Fix Issue #13: Add CORS Support #19

Open tabathadelane opened 4 years ago

tabathadelane commented 4 years ago

name: Adding CORS Support to the API about: Implementing django-cors-headers package and testing it.

What does this do and why?

As mentioned in Issue https://github.com/Pand-Aid/pandaid-api/issues/13, our API needs to set the correct headers to be accessible to the front end.

I added the django-cors-headers package to requirements.txt and followed the setup detailed at https://github.com/adamchainz/django-cors-headers/blob/master/README.rst

Additional notes for reviewers

The response headers should be set to ACCESS-CONTROL-ALLOW-ORIGIN: "*" I attempted to write a test to prove this. I've never written a test for this before, as I usually trouble-shoot CORS in the browser terminal or Postman. I sent up a test designed to replicate that by sending a POST request to 'api-token-auth/" as is mentioned in the main README setup. https://github.com/Pand-Aid/pandaid-api/blob/master/README.md

I followed the format of the previously written tests within the User module. It implemented UserFactory for temporary test users as well as Nosetests. I did my best putting a test together using these libraries.

However, I cannot get the tests to run to see if they are being used correctly.

I also wrote a very simple test that is designed to fail, so I could easily see when tests were running.

Commands Used and Terminal Feedback:

~pandaid-api/pandaid-rest-api/users/        
$ winpty docker exec -it pandaid-api_web_1 //bin/sh
# python manage.py test 

The test command returns this in the bash terminal:

OK!  0 tests, 0 failures, 0 errors in 0.0s

and triggers this is the Docker terminal:

FATAL: database "test_postgres" does not exist

Pre-Review Tasks

Place an ‘x’ between the brackets.

[ x] I wrote tests covering these changes

[ ] I reviewed my own PR before asking for review from others

[ ] I added PR comments providing additional context for reviewers where appropriate, or calling attention to especially sensitive changes.

[ ] If deps were added, I verified the licenses

znmeb commented 4 years ago

Is there a way we can test this on our laptops? I have both Docker for Windows and Docker on Linux. I also have Postman on Linux - perhaps we could use newman scripts to test it.

https://github.com/postmanlabs/newman

tabathadelane commented 4 years ago

I used windows for this. I don't know what the next steps should be.

znmeb commented 4 years ago

I used windows for this. I don't know what the next steps should be.

If you can point me at the Postman testing instructions I'll try to make a newman script to reproduce it. Then we can integrate it somehow for future tests.

tabathadelane commented 4 years ago

This was in the README:

You can get an authentication token by sending a POST request to http://localhost:8000/api-token-auth/ with the superuser credentials in a formdata labeled username and password.

For example, you can send the POST request as a cURL request:

curl --location --request POST 'localhost:8000/api-token-auth/' \ --header 'Content-Type: multipart/form-data' \ --form 'username=' \ --form 'password=' or via an app such as Postman.

tabathadelane commented 4 years ago

Awesome! Are you using the libraries that Emily installed to create a temporary user, or do you want regular Django commands?

znmeb commented 4 years ago

I can use Emily's libraries if there's some documentation that tells me how to do it - no point in adding code or documentation if it's there already somewhere.

tabathadelane commented 4 years ago

Okay. This package is a lot, but it is implemented in /users/factories.py. It's called Factory Boy. I imported into my test_headers.py, but I guess a new Admin class should be created and then used for this test.

Here is the official documentation: https://factoryboy.readthedocs.io/en/latest/internals.html#internals

Also, the top answer here seems to using a really simplified method of creating a superuser. https://stackoverflow.com/questions/15616277/how-can-you-create-an-admin-user-with-factory-boy

I overlooked the superuser requirement. I hadn't gotten this far. I can't get the actual tests to run. the Django shell says it successfully ran 0 tests, 0 failures. I tried "nosetests," but it says it is invalid. I couldn't find any other command online that would run.