Skip to content

Commit

Permalink
Merge pull request #321 from pretendWhale/2.1.1
Browse files Browse the repository at this point in the history
2.1.1
  • Loading branch information
pretendWhale authored Feb 4, 2022
2 parents 799c5b1 + 49f8840 commit 098ff22
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 20 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# CHANGELOG
All notable changes to this project will be documented here.

## [v2.1.1]
- Remove the requirement for clients to send unique user name (#318)

## [v2.1.0]
- Add R tester (#310)

Expand Down
16 changes: 5 additions & 11 deletions client/autotest_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ def _check_rate_limit(user_name):

def _authorize_user():
api_key = request.headers.get("Api-Key")
user_name = (_redis_connection().hgetall("autotest:users") or {}).get(api_key)
user_name = (_redis_connection().hgetall("autotest:user_credentials") or {}).get(api_key)
if user_name is None:
abort(make_response(jsonify(message="Unauthorized"), 401))
_check_rate_limit(user_name)
return user_name
return api_key


def _authorize_settings(user, settings_id=None, **_kw):
Expand Down Expand Up @@ -175,19 +175,13 @@ def _f(*args, **kwargs):
@app.route("/register", methods=["POST"])
def register():
# non-secure registration
user_name = request.json["user_name"]
auth_type = request.json.get("auth_type")
credentials = request.json.get("credentials")
users = _redis_connection().hgetall("autotest:users") or {}
if user_name in users:
abort(make_response(jsonify(message="User already exists"), 400))
key = base64.b64encode(os.urandom(24)).decode("utf-8")
while key in users:
key = base64.b64encode(os.urandom(24)).decode("utf-8")
data = {"auth_type": auth_type, "credentials": credentials}
_redis_connection().hset("autotest:users", key=key, value=user_name)
_redis_connection().hset("autotest:user_credentials", key=user_name, value=json.dumps(data))
return {"user_name": user_name, "api_key": key}
while not _redis_connection().hsetnx("autotest:user_credentials", key=key, value=json.dumps(data)):
key = base64.b64encode(os.urandom(24)).decode("utf-8")
return {"api_key": key}


@app.route("/reset_credentials", methods=["PUT"])
Expand Down
26 changes: 18 additions & 8 deletions client/autotest_client/tests/test_flask_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import autotest_client
import pytest
import fakeredis
import json


@pytest.fixture
Expand All @@ -27,11 +28,20 @@ def fake_redis_db(monkeypatch, fake_redis_conn):


class TestRegister:
def test_no_username(self, client):
resp = client.post("/register")
assert resp.status_code == 500
assert resp.json['message'] == "'NoneType' object is not subscriptable"

def test_with_username(self, client, fake_redis_conn):
client.post("/register", json={"user_name": "test"})
assert "test" in fake_redis_conn.hgetall("autotest:users").values()

@pytest.fixture
def credentials(self):
return {'auth_type': 'test', 'credentials': '12345'}

@pytest.fixture
def response(self, client, credentials):
return client.post("/register", json=credentials)

def test_status(self, response):
assert response.status_code == 200

def test_api_key_set(self, response):
assert response.json['api_key']

def test_credentials_set(self, response, fake_redis_conn, credentials):
assert json.loads(fake_redis_conn.hget('autotest:user_credentials', response.json['api_key'])) == credentials
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def pytest_collect_file(self, parent, path):


class IpynbFile(pytest.File):
TEST_PATTERN = re.compile(r"(?i)^\s*#+\s*(test\w*)\s*$")
TEST_PATTERN = re.compile(r"(?i)^\s*#+\s*(test.*?)\s*$")

def collect(self):
mod = importer.import_from_path(self.fspath)
Expand Down

0 comments on commit 098ff22

Please sign in to comment.