예제 #1
0
def test_login_success(app, db):
    """
    Given I provide valid email and password
    When I make a call to the `login` API
    Then I get HTTP 200 OK response
    And the response body match the schema
    And the response contains user's data
    """
    data = {
        "email": "*****@*****.**",
        "password": "******"
    }
    User(
        email=data["email"],
        password=User.create_hash(plain_password=data["password"])
    ).save()

    client = app.test_client()
    response = client.post("/api/users/login/", data=json.dumps(data))
    assert response.status_code == 200
    response_data = json.loads(response.data.decode("utf-8"))
    validate_dict_with_schema(
        response_data,
        "user/register/response"
    )
    assert response_data["data"]["user"]["email"] == data["email"]
예제 #2
0
def login():
    """
    Log in the user
    """
    errors = None
    try:
        request_data = json.loads(request.data)
        validate_dict_with_schema(request_data, "user/register/request")
        user = User.check_user(
            email=request_data["email"],
            password=request_data["password"]
        )
        if user:
            session["user_id"] = user.id
            return json_response(
                status=200,
                response_data={
                    "success": True, "data": {"user": user.serialize()}
                }
            )
    except (TypeError, ValueError):
        errors = ["Invalid JSON"]
    except ValidationError as e:
        errors = e.message

    if errors:
        return json_response(
            status=400, response_data={"success": False, "errors": errors}
        )

    return json_response(
        status=401,
        response_data={"success": False, "errors": ["Invalid email/password"]}
    )
예제 #3
0
def register():
    """
    Register a new user
    """
    try:
        request_data = json.loads(request.data)
        validate_dict_with_schema(request_data, "user/register/request")
        user = User(
            email=request_data["email"],
            password=User.create_hash(plain_password=request_data["password"])
        )
        user.save()
        return json_response(
            status=200,
            response_data={"success": True,  "data": {"user": user.serialize()}}
        )
    except (TypeError, ValueError):
        errors = ["Invalid JSON"]
    except ValidationError as e:
        errors = e.message
    except IntegrityError:
        errors = ["This email address is already registered"]

    if errors:
        return json_response(
            status=400, response_data={"success": False, "errors": errors}
        )
예제 #4
0
def test_login_wrong_data(app, db):
    """
    Given I don't provide data in a valid format
    When I make a call to the `login` API
    Then I get HTTP 400 BAD REQUEST response
    """
    client = app.test_client()
    response = client.post("/api/users/login/", data=json.dumps({}))
    assert response.status_code == 400
    response_data = json.loads(response.data.decode("utf-8"))
    validate_dict_with_schema(response_data, "error_response")
예제 #5
0
def test_logout_success(app):
    """
    Given that I am logged in
    When I make a call to `logout` API
    Then I get HTTP 200 OK response
    """
    client = app.test_client()
    response = client.post("/api/users/logout/")
    assert response.status_code == 200
    response_data = json.loads(response.data.decode("utf-8"))
    validate_dict_with_schema(
        response_data,
        "user/logout/response"
    )
예제 #6
0
def test_register_short_password(app):
    """
    Given I do not provide valid password
    When I make a call to the `register` API
    Then I get HTTP 400 BAD REQUEST response
    """
    data = {
        "email": "*****@*****.**",
        "password": "******"
    }
    client = app.test_client()
    response = client.post("/api/users/register/", data=json.dumps(data))
    assert response.status_code == 400
    response_data = json.loads(response.data.decode("utf-8"))
    validate_dict_with_schema(response_data, "error_response")
    assert "'test' is too short" in response_data["errors"]
예제 #7
0
def test_register_invalid_email(app):
    """
    Given I do not provide valid email
    When I make a call to the `register` API
    Then I get HTTP 400 BAD REQUEST response
    """
    data = {
        "email": "not-an-email",
        "password": "******"
    }
    client = app.test_client()
    response = client.post("/api/users/register/", data=json.dumps(data))
    assert response.status_code == 400
    response_data = json.loads(response.data.decode("utf-8"))
    validate_dict_with_schema(response_data, "error_response")
    assert "'not-an-email' is not a 'email'" in response_data["errors"]
예제 #8
0
def test_login_not_existing(app, db):
    """
    Given I provide email that is not registered
    When I make a call to the `login` API
    Then I get HTTP 401 UNAUTHORIZED response
    """
    data = {
        "email": "*****@*****.**",
        "password": "******"
    }

    client = app.test_client()
    response = client.post("/api/users/login/", data=json.dumps(data))
    assert response.status_code == 401
    response_data = json.loads(response.data.decode("utf-8"))
    validate_dict_with_schema(response_data, "error_response")
    assert "Invalid email/password" in response_data["errors"]
예제 #9
0
def test_register_existing_email(app, db):
    """
    Given I provide email that is already registered
    When I make a call to the `register` API
    Then I get HTTP 400 BAD REQUEST response
    """
    data = {
        "email": "*****@*****.**",
        "password": "******"
    }
    user = User(**data)
    user.save()

    client = app.test_client()
    response = client.post("/api/users/register/", data=json.dumps(data))
    assert response.status_code == 400
    response_data = json.loads(response.data.decode("utf-8"))
    validate_dict_with_schema(response_data, "error_response")
    assert "This email address is already registered" in response_data["errors"]
예제 #10
0
def test_list_success(app, db):
    """
    Given I have two users in the database
    When I make a call to the `users` API
    Then I get HTTP 200 OK response
    And the response body match the schema
    And the response contains data for two users
    """
    for user in [
        User(email=f"a{n}@example.com", password="******") for n in range(2)
    ]:
        user.save()

    client = app.test_client()
    response = client.get("/api/users/")
    assert response.status_code == 200
    response_data = json.loads(response.data.decode("utf-8"))
    validate_dict_with_schema(
        response_data,
        "user/list/response"
    )
    assert len(response_data["data"]["users"]) == 2
예제 #11
0
def test_register_success(app, db):
    """
    Given I provide valid email and password
    When I make a call to the `register` API
    Then I get HTTP 200 OK response
    And the response body match the schema
    And the response contains new user's data
    And the new user is in the db
    """
    data = {
        "email": "*****@*****.**",
        "password": "******"
    }

    client = app.test_client()
    response = client.post("/api/users/register/", data=json.dumps(data))
    assert response.status_code == 200
    response_data = json.loads(response.data.decode("utf-8"))
    validate_dict_with_schema(
        response_data,
        "user/register/response"
    )
    assert response_data["data"]["user"]["email"] == data["email"]