Skip to content

Commit

Permalink
refactor and first tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardKoschicek committed Jan 23, 2025
1 parent 8ac77c7 commit b71c429
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
3 changes: 2 additions & 1 deletion openatlas/models/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def authorize_all_tokens() -> None:

@staticmethod
def delete_invalid_tokens() -> None:
return db.delete_invalid_tokens([user.id for user in User.get_all() if not user.active])
return db.delete_invalid_tokens(
[user.id for user in User.get_all() if not user.active])

@staticmethod
def is_valid(token: dict[str, Any], user: User) -> bool:
Expand Down
7 changes: 4 additions & 3 deletions openatlas/views/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from datetime import datetime, timedelta
from typing import Any

from flask import flash, g, make_response, redirect, render_template, request, \
url_for
from flask import (
flash, g, make_response, redirect, render_template, request, url_for)
from flask_babel import lazy_gettext as _
from flask_login import login_required
from flask_wtf import FlaskForm
Expand All @@ -30,7 +30,8 @@ class GenerateTokenForm(FlaskForm):
expiration = IntegerField(
_('expiration'),
default=30,
description=_('expiration in days') + ', 0 = ' + _("no expiration date"))
description=
_('expiration in days') + ', 0 = ' + _("no expiration date"))
token_name = StringField(
_('token name'),
default=f"Token_{datetime.today().strftime('%Y-%m-%d')}")
Expand Down
26 changes: 26 additions & 0 deletions tests/test_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from pathlib import Path

from flask import g, url_for

from openatlas import app
from openatlas.database import entity as db
from openatlas.forms.util import form_to_datetime64
from openatlas.models.entity import Link
from tests.base import TestBaseCase, get_hierarchy, insert


class TokenTests(TestBaseCase):
def test_token(self) -> None:
c = self.client
with app.test_request_context():
app.preprocess_request()


rv = c.get(url_for('api_token'))
assert b'Token' in rv.data

rv = c.get(url_for('generate_token'))
assert b'Generate token' in rv.data

rv = c.post(url_for('generate_token'))
assert b'Generate token' in rv.data

0 comments on commit b71c429

Please sign in to comment.