Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.

Commit b34d05a

Browse files
author
Carlos Villavicencio
committed
Added tests
1 parent 3abd1b2 commit b34d05a

File tree

5 files changed

+66
-3
lines changed

5 files changed

+66
-3
lines changed

.github/workflows/flask.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ jobs:
2525
run: |
2626
python -m pip install --upgrade pip
2727
pip install -r requirements.txt
28-
- name: Set up project
28+
- name: Test
2929
run: |
30-
flask init-db
30+
python -m pytest
3131
env:
3232
FLASK_APP: verify
3333
FLASK_ENV: development

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
flask==1.1.1
22
python-dotenv==0.12.0
3-
twilio==6.38.0
3+
twilio==6.38.0
4+
pytest==5.4.1

tests/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import os
2+
import tempfile
3+
4+
import pytest
5+
6+
from verify import create_app
7+
from verify.db import init_db
8+
9+
10+
@pytest.fixture
11+
def app():
12+
db_fd, db_path = tempfile.mkstemp()
13+
app = create_app({'TESTING': True, 'DATABASE': db_path})
14+
with app.app_context():
15+
init_db()
16+
17+
yield app
18+
19+
os.close(db_fd)
20+
os.unlink(db_path)
21+
22+
@pytest.fixture
23+
def client(app):
24+
return app.test_client()

tests/test_auth.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
import re
3+
from unittest.mock import patch
4+
5+
6+
sample_user = {
7+
'username': 'tanjiro',
8+
'password': 'nezuko',
9+
'full_phone': '0909090909',
10+
'channel': 'sms'
11+
}
12+
13+
@patch('verify.auth.start_verification')
14+
def test_creates_an_user(app, client):
15+
res = client.post('/auth/register', data=sample_user)
16+
assert res.status_code == 302
17+
assert '/auth/verify' in res.location
18+
19+
@patch('verify.auth.start_verification')
20+
def test_username_exists_raises_failure(app, client):
21+
client.post('/auth/register', data=sample_user)
22+
res = client.post('/auth/register', data=sample_user)
23+
assert res.status_code == 200
24+
assert re.search('User tanjiro is already registered.', res.get_data(as_text=True))
25+
26+
@patch('verify.auth.start_verification')
27+
def test_create_user_logout_and_log_back_in(app, client):
28+
client.post('/auth/register', data=sample_user)
29+
res = client.get('/auth/logout')
30+
assert res.status_code == 302
31+
assert '/auth/login' in res.location
32+
33+
res = client.post('/auth/login', data=dict(username='tanjiro', password='nezuko'), follow_redirects=True)
34+
assert re.search('Congratulations, you have accessed the secret content!', res.get_data(as_text=True))

tests/test_routes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def test_index(app, client):
2+
res = client.get('/')
3+
assert res.status_code == 302
4+
assert '/auth/login' in res.location

0 commit comments

Comments
 (0)