Skip to content

Commit

Permalink
Add pytest files
Browse files Browse the repository at this point in the history
  • Loading branch information
clayoster committed Sep 16, 2024
1 parent 88b67db commit 19f4764
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Set Environment Variables
os.environ["AUTH_USER"] = "testuser"
os.environ["AUTH_PASS"] = "testpass"

import pytest
import os
from app import app as flask_app

@pytest.fixture
def app():
yield flask_app

@pytest.fixture
def client(app):
return app.test_client()
36 changes: 36 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import base64
import os

def test_env_vars():
assert os.environ["AUTH_USER"] == "testuser"
assert os.environ["AUTH_PASS"] == "testpass"

def basic_auth(username, password):
credentials = f"{username}:{password}"
auth_header = base64.b64encode(credentials.encode()).decode('utf-8')
return {'Authorization': f'Basic {auth_header}'}

def test_good_auth(app, client):
auth_headers = basic_auth('testuser', 'testpass')
res = client.get('/update?hostname=test.domain.com&ip=0.0.0.0', headers=auth_headers)
assert b'noapitoken' in res.data
assert res.status_code == 200

def test_bad_auth(app, client):
auth_headers = basic_auth('testuser', 'badpass')
res = client.get('/update?hostname=test.domain.com&ip=0.0.0.0', headers=auth_headers)
assert b'badauth' in res.data
assert res.status_code == 401


def test_missing_hostname(app, client):
auth_headers = basic_auth('testuser', 'testpass')
res = client.get('/update?ip=0.0.0.0', headers=auth_headers)
assert b'nohost' in res.data
assert res.status_code == 200

def test_missing_ip(app, client):
auth_headers = basic_auth('testuser', 'testpass')
res = client.get('/update?hostname=test.domain.com', headers=auth_headers)
assert b'noip' in res.data
assert res.status_code == 200

0 comments on commit 19f4764

Please sign in to comment.