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_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_other_user_with_superuser(self): # Arrange u = utils.create_and_save_user('u1', 'pass_u1') m = utils.create_user_admin() token = m.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_create_trip_non_existent_user_id(self): # Arrange user_admin = utils.create_user_admin('u1', 'pu1') trip_dict = { 'destination': 'Country1', 'start_date': '01/01/2017', 'end_date': '01/02/2017', 'comment': 'Very Nice', 'user_id': '5' } token = user_admin.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, 404)
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)