"""
:Copyright: 2006-2021 Jochen Kupperschmidt
:License: Revised BSD (see `LICENSE` file for details)
"""

import pytest

from byceps.database import db
from byceps.services.authorization import service as authorization_service
from byceps.services.user import service as user_service
from byceps.services.verification_token.models import Purpose, Token

from tests.helpers import http_client


@pytest.fixture(scope='module')
def user1(make_user):
    return make_user('EAC-User1', initialized=False)


@pytest.fixture(scope='module')
def user2(make_user):
    return make_user('EAC-User2', initialized=False)


@pytest.fixture
def role(admin_app, site, user1, user2):
    role = authorization_service.create_role('board_user', 'Board User')

    yield role

    for user in user1, user2:
        authorization_service.deassign_all_roles_from_user(user.id)

    authorization_service.delete_role(role.id)


def test_confirm_email_address_with_valid_token(site_app, user1, role):
    user_id = user1.id

    user_before = user_service.get_db_user(user_id)
    assert not user_before.email_address_verified
    assert not user_before.initialized

    verification_token = create_confirmation_token(user_id)
    db.session.add(verification_token)
    db.session.commit()

    # -------------------------------- #

    response = confirm(site_app, verification_token)

    # -------------------------------- #

    assert response.status_code == 302

    user_after = user_service.get_db_user(user_id)
    assert user_before.email_address_verified
    assert user_after.initialized

    assert get_role_ids(user_id) == {'board_user'}


def test_confirm_email_address_with_unknown_token(site_app, site, user2, role):
    user_id = user2.id

    user_before = user_service.get_db_user(user_id)
    assert not user_before.initialized

    verification_token = create_confirmation_token(user_id)
    verification_token.token = 'wZdSLzkT-zRf2x2T6AR7yGa3Nc_X3Nn3F3XGPvPtOhw'

    # -------------------------------- #

    response = confirm(site_app, verification_token)

    # -------------------------------- #

    assert response.status_code == 404

    user_after = user_service.get_db_user(user_id)
    assert not user_after.initialized

    assert get_role_ids(user_id) == set()


# helpers


def confirm(app, verification_token):
    url = f'/users/email_address/confirmation/{verification_token.token}'
    with http_client(app) as client:
        return client.get(url)


def get_role_ids(user_id):
    return authorization_service.find_role_ids_for_user(user_id)


def create_confirmation_token(user_id):
    purpose = Purpose.email_address_confirmation
    return Token(user_id, purpose)

@@ -119,39 +119,8 @@
 st%0A%0A
-from byceps.database import db%0A
 from
@@ -290,23 +290,16 @@
 oken
-.models
  import 
 Purp
@@ -298,22 +298,54 @@
 ort 
-Purpose, Token
+(%0A    service as verification_token_service,%0A)
 %0A%0Afr
@@ -1133,37 +1133,24 @@
 alized%0A%0A    
-verification_
 token = crea
@@ -1183,71 +1183,8 @@
 _id)
-%0A    db.session.add(verification_token)%0A    db.session.commit()
 %0A%0A  
@@ -1252,37 +1252,24 @@
 m(site_app, 
-verification_
 token)%0A%0A    
@@ -1735,87 +1735,16 @@
     
-verification_token = create_confirmation_token(user_id)%0A    verification_token.
+unknown_
 toke
@@ -1865,35 +1865,30 @@
 m(site_app, 
-verificatio
+unknow
 n_token)%0A%0A  
@@ -2130,29 +2130,16 @@
 rm(app, 
-verification_
 token):%0A
@@ -2189,27 +2189,8 @@
 on/%7B
-verification_token.
 toke
@@ -2405,26 +2405,54 @@
     
-purpose = Purpose.
+token = verification_token_service.create_for_
 emai
@@ -2477,40 +2477,51 @@
 tion
+(
 %0A    
-return Token(user_id, purpose)
+    user_id%0A    )%0A    return token.token
 %0A

Refactor verification token creation in tests