import bdd_auth_service_api
from common import env

from pytest_bdd import scenarios, given, when, then, parsers


scenarios('../features/register.feature')


@given(parsers.parse('user "{user}" is already registered'))
def user_is_already_registered(env, user):
    r = bdd_auth_service_api.register_to_service(
        env['service_addr'], env['test_id'] + user, "valid-password")
    assert r.status_code == 201


@when(parsers.parse('new user registers with "{user}" username and valid password'))
def new_user_registers_with_valid_password(env, user):
    env['response'] = bdd_auth_service_api.register_to_service(
        env['service_addr'], env['test_id'] + user, "valid-password")


@then('he is registered')
def he_is_registered(env):
    assert env['response'].status_code == 201  # Created


@then('he receives message about already used username')
def he_receives_message_about_already_used_username(env):
    assert env['response'].status_code == 409  # Conflict
