def test_delete(): Request.objects.pending_requests.clear() fake_request = fake_request_creation(False) req = Request.objects.create_request(**fake_request) req_id = req.id assert req in Request.objects.pending_requests Request.objects.delete_request(req) assert req not in Request.objects.pending_requests assert not Request.objects.filter(id=req_id).exists()
def test_delete_other_people(create_full_user, client, has_start_time): user, password = create_full_user fake_request = fake_request_creation(has_start_time) request = Request.objects.create_request(**fake_request) request_id = request.id resp = client.delete(f'/api/v1/request?request_id={request_id}', HTTP_AUTHORIZATION=encode_auth(user.email, password)) assert resp.status_code == 403 assert Request.objects.filter(id=request_id).exists()
def test_fail_no_request(create_full_user, client, has_start_time): user, password = create_full_user req_dict = fake_request_creation(has_start_time) req = Request.objects.create_request(**req_dict) data = {'request_id': req.id + 1, 'comment': fake.text()} resp = client.post('/api/v1/create_response', data=data, content_type='application/json', HTTP_AUTHORIZATION=encode_auth(user.email, password)) assert resp.status_code == 404
def test_delete_success(create_full_user, client, has_start_time): user, password = create_full_user fake_request = fake_request_creation(has_start_time) fake_request['customer'] = user request = Request.objects.create_request(**fake_request) request_id = request.id resp = client.delete(f'/api/v1/request?request_id={request_id}', HTTP_AUTHORIZATION=encode_auth(user.email, password)) assert resp.status_code == 200 assert not Request.objects.filter(id=request_id).exists()
def test_delete_not_in_queue(): Request.objects.pending_requests.clear() orig_queue = Request.objects.pending_requests fake_request = fake_request_creation(True) req = Request.objects.create_request(**fake_request) req_id = req.id assert req not in Request.objects.pending_requests Request.objects.delete_request(req) assert req not in Request.objects.pending_requests assert not Request.objects.filter(id=req_id).exists() assert Request.objects.pending_requests is orig_queue
def test_fail_not_exist(create_full_user, client): Request.objects.paired_requests = {} Request.objects.pending_requests.clear() user, password = create_full_user request_dict = fake_request_creation(False) request = Request.objects.create_request(**request_dict) resp = client.get( f'/api/v1/find_worker?request_id={request.id + 1}', HTTP_AUTHORIZATION=encode_auth(user.email, password) ) assert resp.status_code == 404
def test_query_radius(lat, long, radius, expected, client, create_full_user): request_dict_lst = [ fake_request_creation(True), fake_request_creation(False), ] reqeusts = [Request.objects.create_request(**d) for d in request_dict_lst] reqeusts[0].latitude = 0 reqeusts[0].longitude = 0 reqeusts[0].save() reqeusts[1].latitude = -50 reqeusts[1].longitude = 50 reqeusts[1].save() user, password = create_full_user resp = client.get(f'/api/v1/request?lat={lat}&long={long}&radius={radius}', 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'])
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
def test_not_found(create_full_user, client): Request.objects.paired_requests = {} Request.objects.pending_requests.clear() user, password = create_full_user request_dict = fake_request_creation(False) request_dict['customer'] = user request = Request.objects.create_request(**request_dict) resp = client.get( f'/api/v1/find_worker?request_id={request.id}', HTTP_AUTHORIZATION=encode_auth(user.email, password) ) assert resp.status_code == 200 resp_json = json.loads(resp.content) assert resp_json['worker'] is None
def test_success(create_full_user, client, has_start_time): user, password = create_full_user req_dict = fake_request_creation(has_start_time) req = Request.objects.create_request(**req_dict) actual_json = request_converter.to_dict(req) data = {'request_id': req.id, 'comment': fake.text()} resp = client.post('/api/v1/create_response', data=data, content_type='application/json', HTTP_AUTHORIZATION=encode_auth(user.email, password)) assert resp.status_code == 200 resp_json = json.loads(resp.content) actual_json['responses'].append({ 'worker': user_converter.to_dict(user), 'comment': data['comment'] }) for response in resp_json['responses']: del response['response_id'] assert resp_json == actual_json
def test_success(): Request.objects.pending_requests.clear() fake_request = fake_request_creation(True) req = Request.objects.create_request(**fake_request) assert req assert req not in Request.objects.pending_requests
def test_success_time_null(): Request.objects.pending_requests.clear() fake_request = fake_request_creation(False) req = Request.objects.create_request(**fake_request) assert req assert req in Request.objects.pending_requests