Skip to content

Commit

Permalink
add /api/version endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
zeratax committed Dec 12, 2020
1 parent c59d04e commit ac9c5f2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
19 changes: 17 additions & 2 deletions matrix_registration/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from requests import exceptions
import re
from urllib.parse import urlparse
import os

# Third-party imports...
from datetime import datetime
Expand All @@ -26,6 +27,7 @@
from .matrix_api import create_account
from . import config
from . import tokens
from .constants import __location__

auth = HTTPTokenAuth(scheme='SharedSecret')
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -216,7 +218,20 @@ def register():
base_url=config.config.base_url)


@api.route('/token', methods=['GET', 'POST'])
@api.route('/api/version')
@auth.login_required
def version():
with open(os.path.join(__location__, '__init__.py'), 'r') as file:
version_file = file.read()
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
version_file, re.M)
resp = {
'version': version_match.group(1),
}
return make_response(jsonify(resp), 200)


@api.route('/api/token', methods=['GET', 'POST'])
@auth.login_required
def token():
tokens.tokens.load()
Expand Down Expand Up @@ -251,7 +266,7 @@ def token():
abort(400)


@api.route('/token/<token>', methods=['GET', 'PATCH'])
@api.route('/api/token/<token>', methods=['GET', 'PATCH'])
@auth.login_required
def token_status(token):
tokens.tokens.load()
Expand Down
26 changes: 13 additions & 13 deletions tests/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ def test_get_tokens(self):

secret = matrix_registration.config.config.admin_secret
headers = {'Authorization': 'SharedSecret %s' % secret}
rv = self.client.get('/token', headers=headers)
rv = self.client.get('/api/token', headers=headers)

self.assertEqual(rv.status_code, 200)
token_data = json.loads(rv.data.decode('utf8').replace("'", '"'))
Expand All @@ -492,7 +492,7 @@ def test_error_get_tokens(self):
secret = matrix_registration.config.config.admin_secret
matrix_registration.config.config = Config(GOOD_CONFIG)
headers = {'Authorization': 'SharedSecret %s' % secret}
rv = self.client.get('/token', headers=headers)
rv = self.client.get('/api/token', headers=headers)

self.assertEqual(rv.status_code, 401)
token_data = json.loads(rv.data.decode('utf8').replace("'", '"'))
Expand All @@ -515,7 +515,7 @@ def test_post_token(self, expiration_date, max_usage, parsed_date):

secret = matrix_registration.config.config.admin_secret
headers = {'Authorization': 'SharedSecret %s' % secret}
rv = self.client.post('/token',
rv = self.client.post('/api/token',
data=json.dumps(dict(expiration_date=expiration_date,
max_usage=max_usage)),
content_type='application/json',
Expand All @@ -538,7 +538,7 @@ def test_error_post_token(self):
secret = matrix_registration.config.config.admin_secret
matrix_registration.config.config = Config(GOOD_CONFIG)
headers = {'Authorization': 'SharedSecret %s' % secret}
rv = self.client.post('/token',
rv = self.client.post('/api/token',
data=json.dumps(dict(expiration_date='24.12.2020',
max_usage=False)),
content_type='application/json',
Expand All @@ -552,7 +552,7 @@ def test_error_post_token(self):

secret = matrix_registration.config.config.admin_secret
headers = {'Authorization': 'SharedSecret %s' % secret}
rv = self.client.post('/token',
rv = self.client.post('/api/token',
data=json.dumps(dict(expiration_date='2020-24-12',
max_usage=False)),
content_type='application/json',
Expand All @@ -572,7 +572,7 @@ def test_patch_token(self):

secret = matrix_registration.config.config.admin_secret
headers = {'Authorization': 'SharedSecret %s' % secret}
rv = self.client.patch('/token/' + test_token.name,
rv = self.client.patch('/api/token/' + test_token.name,
data=json.dumps(dict(disabled=True)),
content_type='application/json',
headers=headers)
Expand All @@ -593,7 +593,7 @@ def test_error_patch_token(self):
secret = matrix_registration.config.config.admin_secret
headers = {'Authorization': 'SharedSecret %s' % secret}
matrix_registration.config.config = Config(GOOD_CONFIG)
rv = self.client.patch('/token/' + test_token.name,
rv = self.client.patch('/api/token/' + test_token.name,
data=json.dumps(dict(disabled=True)),
content_type='application/json',
headers=headers)
Expand All @@ -605,7 +605,7 @@ def test_error_patch_token(self):

secret = matrix_registration.config.config.admin_secret
headers = {'Authorization': 'SharedSecret %s' % secret}
rv = self.client.patch('/token/' + test_token.name,
rv = self.client.patch('/api/token/' + test_token.name,
data=json.dumps(dict(active=False)),
content_type='application/json',
headers=headers)
Expand All @@ -615,7 +615,7 @@ def test_error_patch_token(self):
self.assertEqual(token_data['errcode'], 'MR_BAD_USER_REQUEST')
self.assertEqual(token_data['error'], 'you\'re not allowed to change this property')

rv = self.client.patch('/token/' + "nicememe",
rv = self.client.patch('/api/token/' + "nicememe",
data=json.dumps(dict(disabled=True)),
content_type='application/json',
headers=headers)
Expand All @@ -640,7 +640,7 @@ def test_get_token(self, expiration_date, max_usage, parsed_date):

secret = matrix_registration.config.config.admin_secret
headers = {'Authorization': 'SharedSecret %s' % secret}
rv = self.client.get('/token/' + test_token.name,
rv = self.client.get('/api/token/' + test_token.name,
content_type='application/json',
headers=headers)

Expand All @@ -657,7 +657,7 @@ def test_error_get_token(self):

secret = matrix_registration.config.config.admin_secret
headers = {'Authorization': 'SharedSecret %s' % secret}
rv = self.client.get('/token/' + 'nice_meme',
rv = self.client.get('/api/token/' + 'nice_meme',
content_type='application/json',
headers=headers)

Expand All @@ -671,7 +671,7 @@ def test_error_get_token(self):
secret = matrix_registration.config.config.admin_secret
headers = {'Authorization': 'SharedSecret %s' % secret}
matrix_registration.config.config = Config(GOOD_CONFIG)
rv = self.client.patch('/token/' + test_token.name,
rv = self.client.patch('/api/token/' + test_token.name,
data=json.dumps(dict(disabled=True)),
content_type='application/json',
headers=headers)
Expand Down Expand Up @@ -707,7 +707,7 @@ def test_config_path(self):
self.assertIsNotNone(matrix_registration.config.config)
os.remove(good_config_path)

# TODO: - tests for /token/<token>
# TODO: - tests for /api/token/<token>
# - a nonce is only valid for 60s


Expand Down

0 comments on commit ac9c5f2

Please sign in to comment.