def test_get_customer(client, db):
    set_up(db)
    password = "******"
    customer = Customer(name="Anakin Skywalker",
                        email="*****@*****.**",
                        password=generate_password_hash(password))

    db.session.add(customer)
    db.session.commit()

    response = client.post('/api/auth/customer/token',
                           json=dict(email=customer.email, password=password),
                           follow_redirects=True)

    assert response.status_code == 200
    assert response.json['access_token'] is not None

    access_token = response.json['access_token']

    headers = build_header(access_token)

    response = client.get('/api/customer',
                          headers=headers,
                          follow_redirects=True)

    assert response.status_code == 200
    assert response.json['id'] == customer.id
    assert response.json['name'] == customer.name
    assert response.json['email'] == customer.email
    assert customer.verify_password(password)
def test_get_customer_by_email(client, db):
    set_up(db)
    user_password = "******"
    customer_password = "******"
    user = User(name='Leia Organa',
                email='*****@*****.**',
                password=generate_password_hash(user_password),
                roles=[
                    UserRole(category=RoleCategory.SUPER_USER),
                    UserRole(category=RoleCategory.ADMIN)
                ])
    customer = Customer(name="Anakin Skywalker",
                        email="*****@*****.**",
                        password=generate_password_hash(customer_password))
    db.session.add(user)
    db.session.add(customer)
    db.session.commit()

    response = client.post('/api/auth/user/token',
                           json=dict(email=user.email, password=user_password),
                           follow_redirects=True)

    assert response.status_code == 200
    assert response.json['access_token'] is not None

    access_token = response.json['access_token']
    headers = build_header(access_token)

    response = client.get(f'/api/customer/email/{customer.email}',
                          headers=headers,
                          follow_redirects=True)

    assert response.status_code == 200
    assert response.json['id'] == customer.id
    assert response.json['name'] == customer.name
    assert response.json['email'] == customer.email
    assert customer.verify_password(customer_password)