This is a basic webservice intended to work with the SoE website. This webservice gives the website the functionality to access internal data that otherwise shouldn't be accessible.
This code needs docker to run. Please install docker in your local environment before proceed. The installation might be different depending on the operating system you are running.
Once docker is installed, go to this folder and build the virtual machine with:
cd <path-to-this-code>
docker build -t soe .
Then execute the following code to run the virtual machine. The webservice will be mapped automatically to the port 80 of our local machine. (ensure no other process is running in the port 80 or it might conflict):
docker run --name soe-test -p 80:5000 -p 3306:3306 -v `pwd`:/storage/app/ soe
Note that the container has been created to run as an application. Once running it will show the standard output and terminating the process will terminate also the container.
To connect to the container we can use (this will allow us to execute commands from inside the container using the bash shell):
docker exec -it soe-test /bin/bash
If you need to rerun the virtual machine then we need to remove it first. Maybe the following commands will help you executing docker run again.
docker stop soe-test
docker rm soe-test
Once the docker instance is up we should see the flask framework running in the port 5000, but this port is automatically mapped with the port 80 of the host.
Usually it's not recommended to write the API documentation in the README.md file as there are better tools to generate documentation from the code, but in this case we will do an exception.
There are some endpoints created around students administration:
- List students information
GET http://localhost/students
- List specific user information:
GET http://localhost/student/<student-id>
- Create a new user
POST http://localhost/student with form-data fields name, surname and birthdate
- Update an existing user
PUT http://localhost/student/<student-id> with form-data fields name, surname and birthdate
- Delete an existing user
DELETE http://localhost/student/<student-id>
This example comes with two different kinds of tests: unit tests and integration/behavioural tests to check every aspect of the code. The unit tests code coverage is not great as it's intended to be kept simple.
The tests are located under tests/ directory.
To run the tests first we have to connect with the container.
Once inside, we can execute from the command line:
cd /storage/app
pytest
The command will output the results of the tests. If any assertion didn't succeed, the system will show the error and a stack trace to know where the problem started.
The tests are located under tests_api/ directory.
The tests are build through a library called frisby and executed using jasmine. The dependencies are in a package.json file and installed through npm
The dependencies of nodejs are usually installed inside a folder called node_modules.
To run the tests first we have to connect with the container. And then execute:
cd /storage/app
./node_modules/jasmine-node/bin/jasmine-node . --verbose
Several improvements can be done to the code:
- Create a Response class that wraps any response from our webservice, so the format is consistent across any endpoint (status, message, etc. might be something present at all of them).
Gorka Guridi [email protected]