mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
Created endpoints for create and delete token.
This commit is contained in:
@@ -1,16 +1,45 @@
|
||||
import uuid
|
||||
from app.dao import tokens_dao
|
||||
|
||||
from app.dao import api_tokens_dao
|
||||
from app.models import ApiToken
|
||||
from app.models import Token
|
||||
|
||||
|
||||
def test_should_create_token(notify_api, notify_db, notify_db_session, sample_service):
|
||||
token = uuid.uuid4()
|
||||
api_token = ApiToken(**{'token': token, 'service_id': sample_service.id})
|
||||
api_token = Token(**{'token': token, 'service_id': sample_service.id})
|
||||
|
||||
api_tokens_dao.save_token_model(api_token)
|
||||
|
||||
all_tokens = api_tokens_dao.get_model_api_tokens()
|
||||
tokens_dao.save_token_model(api_token)
|
||||
|
||||
all_tokens = tokens_dao.get_model_tokens()
|
||||
assert len(all_tokens) == 1
|
||||
assert all_tokens[0].token == str(token)
|
||||
|
||||
|
||||
def test_should_delete_api_token(notify_api, notify_db, notify_db_session, sample_service):
|
||||
token = uuid.uuid4()
|
||||
api_token = Token(**{'token': token, 'service_id': sample_service.id})
|
||||
tokens_dao.save_token_model(api_token)
|
||||
all_tokens = tokens_dao.get_model_tokens()
|
||||
assert len(all_tokens) == 1
|
||||
|
||||
tokens_dao.delete_model_token(all_tokens[0])
|
||||
empty_token_list = tokens_dao.get_model_tokens()
|
||||
assert len(empty_token_list) == 0
|
||||
|
||||
|
||||
def test_should_return_token_for_service(notify_api, notify_db, notify_db_session, sample_service):
|
||||
the_token = str(uuid.uuid4())
|
||||
api_token = Token(**{'token': the_token, 'service_id': sample_service.id})
|
||||
tokens_dao.save_token_model(api_token)
|
||||
token = tokens_dao.get_model_tokens(sample_service.id)
|
||||
assert token.service_id == sample_service.id
|
||||
assert token.token == str(the_token)
|
||||
|
||||
|
||||
def test_delete_model_token_should_remove_token(notify_api, notify_db, notify_db_session, sample_service):
|
||||
api_token = Token(**{'token': str(uuid.uuid4()), 'service_id': sample_service.id})
|
||||
tokens_dao.save_token_model(api_token)
|
||||
all_tokens = tokens_dao.get_model_tokens()
|
||||
assert len(all_tokens) == 1
|
||||
tokens_dao.delete_model_token(all_tokens[0])
|
||||
assert len(tokens_dao.get_model_tokens()) == 0
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import json
|
||||
from app.models import (Service, User, Template)
|
||||
from app.models import (Service, User, Token, Template)
|
||||
from app.dao.services_dao import save_model_service
|
||||
from tests.app.conftest import sample_user as create_sample_user
|
||||
from flask import url_for
|
||||
@@ -263,6 +263,61 @@ def test_delete_service_not_exists(notify_api, notify_db, notify_db_session, sam
|
||||
assert Service.query.count() == 1
|
||||
|
||||
|
||||
def test_create_token_should_return_token_when_successful(notify_api, notify_db, notify_db_session, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
response = client.post(url_for('service.create_token', service_id=sample_service.id),
|
||||
headers=[('Content-Type', 'application/json')])
|
||||
assert response.status_code == 201
|
||||
assert response.get_data is not None
|
||||
saved_token = Token.query.first()
|
||||
assert saved_token.service_id == sample_service.id
|
||||
|
||||
|
||||
def test_create_token_should_expire_the_old_token_and_create_a_new_token(notify_api, notify_db, notify_db_session,
|
||||
sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
response = client.post(url_for('service.create_token', service_id=sample_service.id),
|
||||
headers=[('Content-Type', 'application/json')])
|
||||
assert response.status_code == 201
|
||||
assert len(Token.query.all()) == 1
|
||||
saved_token = Token.query.first()
|
||||
|
||||
response = client.post(url_for('service.create_token', service_id=sample_service.id),
|
||||
headers=[('Content-Type', 'application/json')])
|
||||
assert response.status_code == 201
|
||||
all_tokens = Token.query.all()
|
||||
assert len(all_tokens) == 2
|
||||
for x in all_tokens:
|
||||
if x.id == saved_token.id:
|
||||
assert x.expiry_date is not None
|
||||
else:
|
||||
assert x.expiry_date is None
|
||||
assert x.token is not saved_token.token
|
||||
|
||||
|
||||
def test_create_token_should_return_error_when_service_does_not_exist(notify_api, notify_db, notify_db_session,
|
||||
sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
response = client.post(url_for('service.create_token', service_id=123),
|
||||
headers=[('Content-Type', 'application/json')])
|
||||
assert response.status_code == 404
|
||||
|
||||
|
||||
def test_delete_token(notify_api, notify_db, notify_db_session, sample_service):
|
||||
with notify_api.test_request_context():
|
||||
with notify_api.test_client() as client:
|
||||
client.post(url_for('service.create_token', service_id=sample_service.id),
|
||||
headers=[('Content-Type', 'application/json')])
|
||||
all_tokens = Token.query.all()
|
||||
assert len(all_tokens) == 1
|
||||
response = client.delete(url_for('service.delete_token', service_id=sample_service.id))
|
||||
assert response.status_code == 202
|
||||
assert len(Token.query.all()) == 0
|
||||
|
||||
|
||||
def test_create_template(notify_api, notify_db, notify_db_session, sample_service):
|
||||
"""
|
||||
Tests POST endpoint '/<service_id>/template' a template can be created
|
||||
|
||||
Reference in New Issue
Block a user