def test_to_dict(has_start_time): req = fake_request(has_start_time) resp = fake_response(req) resp_dict = response_converter.to_dict(resp) assert resp.id == resp_dict['response_id'] assert user_converter.to_dict(resp.worker) == resp_dict['worker'] assert resp.comment == resp_dict['comment']
def test_to_dict(has_start, has_assign, create_full_user, response_count, finished): request = fake_request(has_start) request.finished = finished worker, _ = create_full_user if has_assign: request.assigned_to = worker responses = [fake_response(request) for _ in range(response_count)] request.save() request_dict = request_converter.to_dict(request) assert request_dict['request_id'] == request.id assert request_dict['customer'] == user_converter.to_dict(request.customer) if has_start: assert request_dict['start_time'] == int( request.start_time.timestamp()) else: assert request_dict['start_time'] is None assert request_dict['duration'] == int(request.duration.total_seconds()) assert abs(request_dict['latitude'] - float(request.latitude)) < 0.0001 assert abs(request_dict['longitude'] - float(request.longitude)) < 0.0001 assert request_dict['finished'] == request.finished assert request_dict['description'] == request.description if has_assign: assert request_dict['assigned_to'] == user_converter.to_dict(worker) else: assert request_dict['assigned_to'] is None assert len(request_dict['responses']) == len(responses) for response_dict in (response_converter.to_dict(resp) for resp in responses): assert response_dict in request_dict['responses']
def test_fail_job_not_found(create_full_user, has_start_time, client): user, password = create_full_user request = fake_request(has_start_time) resp = client.post('/api/v1/assign_worker', data={'request_id': request.id + 1}, content_type='application/json', HTTP_AUTHORIZATION=encode_auth(user.email, password)) assert resp.status_code == 404 assert request.assigned_to is None
def test_no_query(create_full_user, client): user, password = create_full_user reqeusts = [ fake_request(random.choice([True, False]), user) for _ in range(10) ] other_requests = [ fake_request(random.choice([True, False])) for _ in range(10) ] for req in reqeusts: for _ in range(random.randint(0, 5)): fake_response(req) resp = client.get('/api/v1/request', HTTP_AUTHORIZATION=encode_auth(user.email, password)) assert resp.status_code == 200 resp_dict = json.loads(resp.content) assert sorted(resp_dict['requests'], key=lambda d: d['request_id']) == sorted( (request_converter.to_dict(req) for req in reqeusts), key=lambda d: d['request_id'])
def test_fail_finished(create_full_user, client, has_start_time): user, password = create_full_user request = fake_request(has_start_time) request.finished = True request.save() resp = client.post('/api/v1/assign_worker', data={'request_id': request.id}, content_type='application/json', HTTP_AUTHORIZATION=encode_auth(user.email, password)) assert resp.status_code == 400 assert request.assigned_to is None
def test_assign_success_self(create_full_user, client, has_start_time): user, password = create_full_user request = fake_request(has_start_time) resp = client.post('/api/v1/assign_worker', data={'request_id': request.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 == user assert json.loads(resp.content) == request_converter.to_dict(request)
def test_fail_cannot_assign_self(create_full_user, client, has_start_time, has_id): user, password = create_full_user request = fake_request(has_start_time, user) data = {'request_id': request.id} if has_id: data['worker_id'] = user.id resp = client.post('/api/v1/assign_worker', data=data, content_type='application/json', HTTP_AUTHORIZATION=encode_auth(user.email, password)) assert resp.status_code == 400 assert request.assigned_to is None
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
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)