Skip to content
Snippets Groups Projects
Commit ec3ec96a authored by Victor Löfgren's avatar Victor Löfgren
Browse files

Merge branch 'issue/3-test-back-end' into 'dev'

Issue/3 test back end

See merge request !3
parents 4c846739 4317b5fc
No related branches found
No related tags found
1 merge request!3Issue/3 test back end
__pycache__ __pycache__
*.db *.db
*/env */env
*.coverage *.coverage
\ No newline at end of file .pytest_cache
\ No newline at end of file
stages:
- setup
- test
include:
- local: .gitlab/server.gitlab-ci.yml
image: python
cache:
paths:
- server/env/
before_script:
- python --version
- pip install virtualenv
- cd server/
- python -m venv env
- source env/bin/activate
- pip install -r requirements.txt
test-server:
stage: test
script:
- pytest --cov app tests/
...@@ -29,14 +29,12 @@ ...@@ -29,14 +29,12 @@
"label": "Test Server", "label": "Test Server",
"type": "shell", "type": "shell",
"group": "build", "group": "build",
"command": "${workspaceFolder}/server/env/Scripts/python test.py", "command": "env/Scripts/pytest.exe --cov app tests/",
"problemMatcher": [], "problemMatcher": [],
"options": { "options": {
"cwd": "${workspaceFolder}/server" "cwd": "${workspaceFolder}/server"
}, },
"presentation": {
"group": "Client/Server"
}
}, },
{ {
"label": "Client + Server", "label": "Client + Server",
......
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
from app import create_app, db
import unittest
import json
class Test(unittest.TestCase):
def setUp(self):
"""Before each test, set up a blank database"""
self.app = create_app("configmodule.TestingConfig")
self.app.testing = True
self.client = self.app.test_client()
with self.app.app_context():
db.drop_all()
db.create_all()
# Called after every test
def tearDown(self):
with self.app.app_context():
db.session.remove()
db.drop_all()
def test_user(self):
# Create user
rv = self.client.post(
"/api/users/",
data=json.dumps({"email": "test@test.se", "password": "abc123"}),
)
rv_dict = json.loads(rv.data.decode())
assert rv.status_code == 200
assert rv_dict["id"] == 1
assert "password" not in rv_dict
assert rv_dict["email"] == "test@test.se"
# Try loggin with wrong PASSWORD
rv = self.client.post("/api/users/login", data=json.dumps({"email": "test@test.se", "password": "abc1234"}))
assert rv.status_code == 401
# Try loggin with wrong Email
rv = self.client.post("/api/users/login", data=json.dumps({"email": "test1@test.se", "password": "abc1234"}))
assert rv.status_code == 401
# Try loggin with right PASSWORD
rv = self.client.post("/api/users/login", data=json.dumps({"email": "test@test.se", "password": "abc123"}))
rv_dict = json.loads(rv.data.decode())
assert rv.status_code == 200
headers = {"Authorization": "Bearer " + rv_dict["access_token"]}
# Get the current user
rv = self.client.get("/api/users/", headers=headers)
rv_dict = json.loads(rv.data.decode())
assert rv.status_code == 200
assert rv_dict["email"] == "test@test.se"
rv = self.client.put("/api/users/", data=json.dumps({"name": "carl carlsson"}), headers=headers)
rv_dict = json.loads(rv.data.decode())
assert rv.status_code == 200
assert rv_dict["name"] == "Carl Carlsson"
def test_empty(self):
# Try loggin withou any users
rv = self.client.post("/api/users/login", data=json.dumps({"email": "test@test.se", "password": "abc123"}))
assert rv.status_code == 401
if __name__ == "__main__":
unittest.main()
import pytest
from app import create_app, db
@pytest.fixture
def app():
app = create_app("configmodule.TestingConfig")
with app.app_context():
db.drop_all()
db.create_all()
return app
@pytest.fixture
def client(app):
return app.test_client()
from tests import app, client
import json
def test_app(client):
# Create user
rv = client.post(
"/api/users/",
data=json.dumps({"email": "test@test.se", "password": "abc123"}),
)
rv_dict = json.loads(rv.data.decode())
assert rv.status_code == 200
assert rv_dict["id"] == 1
assert "password" not in rv_dict
assert rv_dict["email"] == "test@test.se"
# Try loggin with wrong PASSWORD
rv = client.post("/api/users/login", data=json.dumps({"email": "test@test.se", "password": "abc1234"}))
assert rv.status_code == 401
# Try loggin with wrong Email
rv = client.post("/api/users/login", data=json.dumps({"email": "test1@test.se", "password": "abc1234"}))
assert rv.status_code == 401
# Try loggin with right PASSWORD
rv = client.post("/api/users/login", data=json.dumps({"email": "test@test.se", "password": "abc123"}))
rv_dict = json.loads(rv.data.decode())
assert rv.status_code == 200
headers = {"Authorization": "Bearer " + rv_dict["access_token"]}
# Get the current user
rv = client.get("/api/users/", headers=headers)
rv_dict = json.loads(rv.data.decode())
assert rv.status_code == 200
assert rv_dict["email"] == "test@test.se"
rv = client.put("/api/users/", data=json.dumps({"name": "carl carlsson"}), headers=headers)
rv_dict = json.loads(rv.data.decode())
assert rv.status_code == 200
assert rv_dict["name"] == "Carl Carlsson"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment