Example #1
0
def test_to_dict():
    for user, _ in generate_users(10):
        user_dict = user_converter.to_dict(user)
        assert user_dict['user_id'] == user.id
        assert user_dict['first_name'] == user.first_name
        assert user_dict['last_name'] == user.last_name
        assert user_dict['bio'] == user.bio
        assert isinstance(user_dict['bio'], str)
        for key in user_dict:
            assert key in ['user_id', 'first_name', 'last_name', 'bio']
Example #2
0
def test_success_query(create_full_user, client):
    my_user, password = create_full_user
    other_users = list(generate_users(10))
    for user, _ in other_users:
        user.save()
    for other, _ in other_users:
        resp = client.get(f'/api/v1/profile?user_id={other.id}',
                          HTTP_AUTHORIZATION=encode_auth(
                              my_user.email, password))
        assert resp.status_code == 200
        assert resp.content.decode() == json.dumps(
            user_converter.to_dict(other))
Example #3
0
def test_query(query, expected, create_full_user, client):
    user, password = create_full_user

    customers = [u for u, _ in generate_users(5)]
    workers = [u for u, _ in generate_users(5)]
    for u in chain(customers, workers):
        u.save()

    default_args = lambda: {
        'duration': fake.time_delta(),
        'latitude': fake.latitude(),
        'longitude': fake.longitude(),
        'description': fake.text(),
        'customer': random.choice(customers)
    }
    reqeusts = [
        Request(start_time=datetime.fromtimestamp(1000),
                assigned_to=random.choice(workers),
                finished=True,
                **default_args()),
        Request(start_time=datetime.fromtimestamp(30000),
                assigned_to=None,
                finished=False,
                **default_args())
    ]
    for r in reqeusts:
        r.clean()
        r.save()
    resp = client.get(f'/api/v1/request?{query}',
                      HTTP_AUTHORIZATION=encode_auth(user.email, password))
    assert resp.status_code == 200
    resp_dict = json.loads(resp.content)
    resp_requests = resp_dict['requests']
    assert len(resp_requests) == len(expected)
    assert sorted([request_converter.to_dict(reqeusts[i]) for i in expected],
                  key=lambda d: d['request_id']) == sorted(
                      resp_requests, key=lambda d: d['request_id'])
Example #4
0
def test_bad_id(create_full_user, client):
    Request.objects.paired_requests = {}
    Request.objects.pending_requests.clear()

    user, password = create_full_user
    worker, _ = random.choice(list(generate_users(3)))

    request_dict = fake_request_creation(False)
    request_dict['customer'] = user
    request = Request.objects.create_request(**request_dict)
    assert Request.objects.find_job(worker)
    resp = client.get(
        f'/api/v1/find_worker?request_id={request.id}.1',
        HTTP_AUTHORIZATION=encode_auth(user.email, password)
    )
    assert resp.status_code == 400
Example #5
0
def test_fail_cannot_assign_others_job(client, has_start_time):
    users = list(generate_users(3))
    user, password = users[0]
    other_user, other_pass = users[1]
    worker, worker_pass = users[2]
    user.save()
    other_user.save()
    worker.save()

    request = fake_request(has_start_time, other_user)
    resp = client.post('/api/v1/assign_worker',
                       data={
                           'request_id': request.id,
                           'worker_id': worker.id
                       },
                       content_type='application/json',
                       HTTP_AUTHORIZATION=encode_auth(user.email, password))

    assert resp.status_code == 403
    assert request.assigned_to is None
Example #6
0
def test_assign_success_other(client, has_start_time):
    users = list(generate_users(2))
    worker, worker_pass = users[0]
    user, password = users[1]
    worker.save()
    user.save()
    request = fake_request(has_start_time, user)

    resp = client.post('/api/v1/assign_worker',
                       data={
                           'request_id': request.id,
                           'worker_id': worker.id
                       },
                       content_type='application/json',
                       HTTP_AUTHORIZATION=encode_auth(user.email, password))

    assert resp.status_code == 200
    request = Request.objects.get(id=request.id)
    assert request.assigned_to == worker
    assert json.loads(resp.content) == request_converter.to_dict(request)