-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
228 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,27 @@ | ||
import pytest,os | ||
from tronbyt_server import create_app | ||
import pytest | ||
import os | ||
from tronbyt_server import create_app, db | ||
|
||
@pytest.fixture() | ||
def app(): | ||
app = create_app(test_config=True) | ||
app.config['TESTING'] = True | ||
|
||
yield app | ||
with app.app_context(): | ||
yield app | ||
|
||
# clean up / reset resources here | ||
# remove the users/testuser directory | ||
os.system("rm -rf tests/users/testuser") | ||
|
||
|
||
print("delete testdb") | ||
os.system("rm users/testdb.sqlite") | ||
|
||
@pytest.fixture() | ||
def client(app): | ||
return app.test_client() | ||
|
||
|
||
@pytest.fixture() | ||
def runner(app): | ||
return app.test_cli_runner() | ||
return app.test_cli_runner() | ||
|
||
@pytest.fixture() | ||
def app_context(app): | ||
with app.app_context(): | ||
yield |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import os | ||
from . import utils | ||
from tronbyt_server import db | ||
|
||
def test_api(client): | ||
|
||
# load the test data (register,login,create device) | ||
device_id = utils.load_test_data(client) | ||
|
||
# push base64 image via call to push | ||
|
||
data = """UklGRsYAAABXRUJQVlA4TLkAAAAvP8AHABcw/wKBJH/ZERYIJEHtr/b8B34K3DbbHievrd+SlSqA3btETOGfo881kEXFGJQRa+biGiCi/xPAXywwVqenXXoCj+L90gO4ryqALawrJOwGX1iVsGnVMRX8irHyqbzGagksXy0zsmlldlEbgotNM1Nfaw04UbmahSFTi0pgml3UgIvaNDNA4JMikAFTQ16YXYhDNk1jbiaGoTEgsnO5vqJ1KwpcpWXOiQrUoqbZyc3FIEb5PAA=""" | ||
|
||
# Create a JSON object with your data | ||
object = { | ||
"image": data, | ||
#"installationId": "test" | ||
} | ||
|
||
# Send the POST request using requests library | ||
url = f"/v0/devices/{device_id}/push" | ||
response = client.post(url, headers={'Authorization': 'aa','Content-Type': 'application/json'}, json=object) | ||
# assert no exist because of bad key | ||
push_path = f"{db.get_device_webp_dir(device_id)}/pushed/" | ||
|
||
assert(not os.path.exists(push_path)) | ||
|
||
# good key | ||
response = client.post(url, headers={'Authorization': 'TESTKEY','Content-Type': 'application/json'}, json=object) | ||
# assert a file starting with __ exist in the web device dir | ||
file_list = [f for f in os.listdir(push_path) if os.path.isfile(os.path.join(push_path, f)) and f.startswith("__")] | ||
assert(len(file_list) > 0) | ||
|
||
# call next | ||
client.get(f"{device_id}/next") | ||
# assert the file is now deleted | ||
file_list = [f for f in os.listdir(push_path) if os.path.isfile(os.path.join(push_path, f)) and f.startswith("__")] | ||
assert(len(file_list) == 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import os | ||
from . import utils | ||
|
||
def test_device_operations(client): | ||
client.post("/auth/register", data={"username": "testuser", "password": "password"}) | ||
client.post("/auth/login", data={"username": "testuser", "password": "password"}) | ||
|
||
r = client.get("/create") | ||
assert r.status_code == 200 | ||
|
||
r = client.post("/create", data={"name":"TESTDEVICE","img_url":"TESTID","api_key":"TESTKEY","notes":"TESTNOTES", "brightness":"30"}) | ||
print(r.text) | ||
assert "TESTDEVICE" in utils.get_testuser_config_string() | ||
|
||
device_id = utils.get_test_device_id() | ||
# Test firmware generation page | ||
r = client.get(f"{device_id}/firmware") | ||
assert(r.status_code == 200) | ||
|
||
# id: device['id'] | ||
# img_url: http://m1Pro.local:8000/9abe2858/next | ||
# wifi_ap: Blah | ||
# wifi_password: Blah | ||
data = { | ||
'id': device_id, | ||
'img_url': f"http://m1Pro.local:8000/{device_id}/next", | ||
'wifi_ap': 'Blah', | ||
'wifi_password': 'Blah' | ||
} | ||
r = client.post(f"/{device_id}/firmware", data = data) | ||
assert(r.status_code == 200) | ||
|
||
# r = client.get(f"/{device_id}/download_firmware") | ||
assert(os.path.exists(f"firmware/gen1_TESTDEVICE.BIN")) | ||
os.remove(f"firmware/gen1_TESTDEVICE.BIN") | ||
|
||
|
||
# test /device_id/next works even when no app configured | ||
assert(client.get(f"{device_id}/next").status_code == 200) | ||
|
||
|
||
# Delete the device. | ||
r = client.post(f"{device_id}/delete") | ||
assert "TESTDEVICE" not in utils.get_testuser_config_string() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
import os | ||
import os,time | ||
|
||
def test_register_login_logout(client): | ||
response = client.get("/auth/register") | ||
assert response.status_code == 200 | ||
client.post("/auth/register", data={"username": "testuser", "password": "password"}) | ||
# assert user.config file exists | ||
assert os.path.exists(os.path.join("tests", "users", "testuser", "testuser.json")) | ||
response = client.post("/auth/register", data={"username": "testuser", "password": "password"}) | ||
# Ensure response is a redirect to /auth/login | ||
|
||
client.post("/auth/login", data={"username": "testuser", "password": "password"}) | ||
response = client.get("/") | ||
assert response.status_code == 200 | ||
# assert response.status_code == 302 | ||
assert response.headers['Location'] == "/auth/login" | ||
|
||
# test successful login of new user | ||
response = client.post("/auth/login", data={"username": "testuser", "password": "password"}) | ||
assert response.status_code == 302 | ||
assert response.headers['Location'] == "/" | ||
|
||
response = client.get("/auth/logout") | ||
assert response.status_code == 302 # should redirect to login | ||
assert "auth/login" in response.text # make sure redirected to auth/login | ||
assert response.headers['Location'] == "/auth/login" # make sure redirected to auth/login | ||
|
||
def test_login_with_wrong_password(client): | ||
response = client.post("/auth/login", data={"username": "testuser", "password": "BADDPASSWORD"}) | ||
response = client.post("/auth/login", data={"username": "testuser", "password": "BADDPASSWORD"}) | ||
print(response.text) | ||
assert "Incorrect username/password." in response.text | ||
|
||
def test_unauth_index(client): | ||
response = client.get("/") | ||
assert response.status_code == 302 # should redirect to login | ||
assert "auth/login" in response.text | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.