def test_create_trip_bad_date_format(self, start_date): # Arrange user = utils.create_and_save_user() trip_dict = { 'destination': 'Country1', 'start_date': start_date, 'end_date': '10/12/2017', 'comment': 'Very Nice', 'user_id': user.id } token = user.generate_rest_auth_token() headers = {'Authorization': utils.encode_info_token_http_auth(token)} # Act response = self.client.post('/api/v1/trips/', data=json.dumps(trip_dict), headers=headers, content_type='application/json') data = json.loads(utils.decode_data(response.data)) # Assert self.assertEqual(response.status_code, 400) self.assertIn( f"time data '{start_date}' does not match format '{tp_utils.DATE_FORMAT}'", data['error'])
def test_get_itinerary_for_next_month(self): # Arrange user_admin = utils.create_user_admin() headers = { 'Authorization': utils.encode_info_token_http_auth( user_admin.generate_rest_auth_token()) } for _ in range(5): today = datetime.date.today() if today.month == 12: next_month = datetime.date(today.year + 1, 1, 1) else: next_month = datetime.date(today.year, today.month + 1, 1) next_month = next_month.strftime("%d/%m/%Y") utils.create_and_save_trip('TEST', next_month, next_month, 'TEST', user_admin) for _ in range(5): today = datetime.date.today().strftime("%d/%m/%Y") utils.create_and_save_trip('TEST', today, today, 'TEST', user_admin) # Act response = self.client.get('/api/v1/trips/next_month/', headers=headers) # Assert self.assertEqual(response.status_code, 200) data = json.loads(utils.decode_data(response.data)) self.assertEqual(len(data), 5)
def test_create_trip(self): # Arrange user = utils.create_and_save_user() trip_dict = { 'destination': 'Country1', 'start_date': '01/01/2017', 'end_date': '01/02/2017', 'comment': 'Very Nice', 'user_id': user.id } token = user.generate_rest_auth_token() headers = {'Authorization': utils.encode_info_token_http_auth(token)} # Act response = self.client.post('/api/v1/trips/', data=json.dumps(trip_dict), headers=headers, content_type='application/json') # Assert self.assertEqual(response.status_code, 201) trip = Trip.query.get( json.loads(utils.decode_data(response.data))['id']) self.assertEqual(trip.start_date, datetime.date(2017, 1, 1)) self.assertEqual(trip.end_date, datetime.date(2017, 2, 1))
def test_filter_trips_by_end_date(self): # Arrange user_admin = utils.create_user_admin() headers = { 'Authorization': utils.encode_info_token_http_auth( user_admin.generate_rest_auth_token()) } # Dict used to insert records. Key is the destination, value is how many # records to insert records_to_insert = {'01/02/2016': 2, '02/02/2016': 3, '03/02/2016': 1} for end_date, count in records_to_insert.items(): for _ in range(count): utils.create_and_save_trip('test dest', '01/02/2016', '{}'.format(end_date), 'Test trip', user_admin) for end_date, count in records_to_insert.items(): query = {'end_date': end_date} # Act response = self.client.post('/api/v1/trips/filter/', data=json.dumps(query), headers=headers, content_type='application/json') # Assert self.assertEqual(response.status_code, 200) data = json.loads(utils.decode_data(response.data)) self.assertEqual(len(data), count)
def test_get_auth_token_success(self): # Arrange user = utils.create_and_save_user() auth_header = utils.encode_info_basic_http_auth(user.username, 'pass1') # Act response = self.client.post('/api/v1/token/', headers={'Authorization': auth_header}) decoded_data = utils.decode_data(response.data) # Assert self.assertIn('token', json.loads(decoded_data))
def test_get_one_user_correct_id(self): # Arrange u = utils.create_and_save_user('u1', 'pass_u1') token = u.generate_rest_auth_token() # Act response = self.client.get('/api/v1/users/{}/'.format(u.id), headers={ 'Authorization': utils.encode_info_token_http_auth(token) }) # Assert self.assertEqual(response.status_code, 200) u_dict = json.loads(utils.decode_data(response.data)) self.assertEqual(u_dict['username'], u.username)
def test_list_trips_admin_user_future_and_past_trips(self): # Arrange user_admin = utils.create_user_admin() for day in [1, 2, 3]: utils.create_and_save_trip('D{}'.format(day), '{}/1/2016'.format(day), '{}/02/2016'.format(day), 'Test trip', user_admin) for day in [1, 2, 3]: utils.create_and_save_trip('D{}'.format(day), '{}/1/2017'.format(day), '{}/02/2017'.format(day), 'Test trip', user_admin) headers = { 'Authorization': utils.encode_info_token_http_auth( user_admin.generate_rest_auth_token()) } today = datetime.date(2016, 12, 20) # Act with mock.patch('datetime.date', utils.MockDate): utils.MockDate.today = classmethod(lambda cls: today) response = self.client.get('/api/v1/trips/', headers=headers, content_type="application/json") # Assert self.assertEqual(response.status_code, 200) date_order = [ '03/01/2017', '02/01/2017', '01/01/2017', '03/01/2016', '02/01/2016', '01/01/2016' ] data = json.loads(utils.decode_data(response.data)) for i in range(3): self.assertTrue(data[i].get('days_left')) self.assertEqual(data[i]['start_date'], date_order[i]) for i in range(3, 6): self.assertFalse(data[i].get('days_left')) self.assertEqual(data[i]['start_date'], date_order[i])
def test_register_user_success(self): # Arrange user = { 'username': '******', 'password': '******', 'first_name': 'Us', 'last_name': 'Er' } # Act response = self.client.post('/api/v1/users/', data=json.dumps(user), content_type='application/json') # Assert decoded_data = utils.decode_data(response.data) self.assertEqual(response.status_code, 201) self.assertEqual( json.loads(decoded_data)['username'], user['username'])
def test_get_all_users_from_admin_success(self): # Arrange admin = utils.create_user_admin() usernames = ['u1', 'u2', 'u3'] for u in usernames: utils.create_and_save_user(u, 'pass{}'.format(u)) usernames.append('admin') token = admin.generate_rest_auth_token() # Act response = self.client.get('/api/v1/users/', headers={ 'Authorization': utils.encode_info_token_http_auth(token) }) # Assert self.assertEqual(response.status_code, 200) users = json.loads(utils.decode_data(response.data)) for u in users: self.assertIn(u['username'], usernames)