class TestRegister(TestCase): def setUp(self): self.user_auth = UserAuthFactory(username="******", password="******" , email="*****@*****.**") self.user_auth.save() self.user = UserProfileFactory(user_auth = self.user_auth , phone = '0123456789', city = 'Eind') self.user.save() self.client = Client() self.factory = RequestFactory() def test_register_valid_user(self): data={ 'username': '******', 'password': '******', 'email': '*****@*****.**', 'first_name': 'dima', 'last_name': 'k', 'city': 'Kiev', 'phone': '123123'} response = self.client.post('/user/register/', data) self.assertEqual(response.status_code, 200) def test_register_already_registered_user(self): data = { 'username': '******', 'password': '******', 'email': '*****@*****.**', 'first_name': 'dima', 'last_name': 'k', 'city': 'Kiev', 'phone': '123123'} response = self.client.post('/user/register/', data) self.assertEqual(response.status_code, 400) def test_register_invalid_form(self): def test_register_alreadyRegistered_user(self): data = { 'usesrname': 'user', 'password': '******', 'email': '*****@*****.**', 'first_name': 'dima', 'last_name': 'k', 'city': 'Kiev', 'phone': '123123'} response = self.client.post('/user/register/', data) self.assertEqual(response.status_code, 400)
class TestEventResource(TestCase): def setUp(self): self.client = Client() self.user_auth = UserAuthFactory(username="******", password="******", email="*****@*****.**") self.user_auth.save() self.user = UserProfileFactory(user_auth=self.user_auth, phone='0123456789', city='Eind') self.user.save() self.event = EventFactory(organizer=self.user) self.event.save() self.food = FoodFactory() self.food.save() self.food_chicken = FoodFactory(food_type="Chicken") self.food_chicken.save() self.event.food_types.add(self.food) self.event.food_types.add(self.food_chicken) def test_get_successfully_all_events(self): self.client.login(username="******", password="******") response = self.client.get('/events/') event_date = str(self.event.event_date) event = response.data['events'][0] self.assertEqual(response.status_code, 200) self.assertEqual(event['name'], self.event.name) self.assertEqual(event['organizer_name'], self.event.organizer.user_auth.get_full_name()) self.assertEqual(event['category'], self.event.category) self.assertEqual(event['number_of_attendees'], 0) self.assertEqual(event['event_date'], event_date) self.assertEqual(len(event['food_types']), 2) def test_get_user_is_not_loggedin(self): response = self.client.get('/events/') self.assertEqual(response.status_code, 401)
class TestEventAttendeeResource(TestCase): def setUp(self): self.client = Client() self.user_auth = UserAuthFactory(username="******", password="******", email="*****@*****.**") self.user_auth.save() self.user = UserProfileFactory(user_auth = self.user_auth, phone = '0123456789', city = 'Eind') self.user.save() self.food = FoodFactory() self.food.save() self.food_chicken = FoodFactory(food_type="Chicken") self.food_chicken.save() self.event = EventFactory(organizer = self.user) self.event.save() self.event.food_types.add(self.food) self.event.food_types.add(self.food_chicken) self.event_date = str(self.event.event_date) self.data = { 'first_name' : 'Pouya', 'last_name' : 'Samadi Khah', 'number_of_guests': '5', 'beef': '4', 'pork': '5' } def test_post_successfully_created_attendee(self): response = self.client.post( f"/user/attend/{self.event_date }/{self.event.name}", json.dumps(self.data), content_type='application/json') self.assertEqual(response.status_code, 200) def test_post_invalid_event_date(self): event_date = str(self.event.event_date - timedelta(days = -1)) response = self.client.post( f"/user/attend/{event_date}/{self.event.name}", json.dumps(self.data), content_type='application/json') self.assertEqual(response.status_code, 404) def test_post_invalid_event_name(self): response = self.client.post( f"/user/attend/{self.event_date }/wrong", json.dumps(self.data), content_type='application/json') self.assertEqual(response.status_code, 404) def test_post_invalid_format_guests(self): self.data['number_of_guests'] = 'invalid' response = self.client.post( f"/user/attend/{self.event_date }/{self.event.name}", json.dumps(self.data), content_type='application/json') self.assertEqual(response.status_code, 400) def test_post_invalid_format_first_name(self): self.data['first_name'] = None response = self.client.post( f"/user/attend/{self.event_date }/{self.event.name}", json.dumps(self.data), content_type='application/json') self.assertEqual(response.status_code, 400) def test_post_invalid_format_last_name(self): self.data['last_name'] = None response = self.client.post( f"/user/attend/{self.event_date }/{self.event.name}", json.dumps(self.data), content_type='application/json') self.assertEqual(response.status_code, 400) def test_post_empty_food_orders_attendee(self): self.data = { 'first_name' : 'Pouya', 'last_name' : 'Samadi Khah', 'number_of_guests': '5', } response = self.client.post( f"/user/attend/{self.event_date }/{self.event.name}", json.dumps(self.data), content_type='application/json') self.assertEqual(response.status_code, 400)
class TestEventInstanceResource(TestCase): def setUp(self): self.client = Client() self.user_auth = UserAuthFactory(username="******", password="******", email="*****@*****.**") self.user_auth.save() self.user = UserProfileFactory(user_auth=self.user_auth, phone='0123456789', city='Eind') self.user.save() self.event = EventFactory(organizer=self.user) self.event.save() self.food = FoodFactory() self.food.save() self.food_chicken = FoodFactory(food_type="Chicken") self.food_chicken.save() self.food_order_1 = FoodOrderFactory(food=self.food_chicken) self.food_order_1.save() self.food_order_2 = FoodOrderFactory(food=self.food) self.food_order_2.save() self.attendee = AttendeeFactory() self.attendee.save() self.event_attendee = EventAttendeeFactory(event=self.event, attendee=self.attendee) self.event_attendee.save() self.event_attendee.food_orders.add(self.food_order_1) self.event_attendee.food_orders.add(self.food_order_2) def test_get_wrong_date_event(self): event_date = str(self.event.event_date - timedelta(days=10)) response = self.client.get("/events/item/" f"{event_date}/{self.event.name}") self.assertEqual(response.status_code, 404) def test_get_wrong_event_name(self): event_date = str(self.event.event_date) response = self.client.get(f"/events/item/{event_date}/wrong") self.assertEqual(response.status_code, 404) def test_get_successfully_event(self): event_date = str(self.event.event_date) response = self.client.get("/events/item/" f"{event_date}/{self.event.name}") self.assertEqual(response.status_code, 200) data = response.data.get('event') self.assertEqual(data['name'], self.event.name) self.assertEqual(data['event_date'], event_date) self.assertEqual(data['organizer_name'], self.event.organizer.user_auth.get_full_name()) self.assertEqual(data['category'], self.event.category) self.assertEqual(data['number_of_attendees'], 11) food_order_number_list = {'chicken': 5, 'pork': 5} self.assertEqual(data['food_order_number_list'], food_order_number_list)
class TestEventItemResource(TestCase): def setUp(self): self.client = Client() self.user_auth = UserAuthFactory(username="******", password="******", email="*****@*****.**") self.user_auth.save() self.user = UserProfileFactory(user_auth=self.user_auth, phone='0123456789', city='Eind') self.user.save() self.event = EventFactory.create(organizer=self.user) self.event.save() self.food = FoodFactory() self.food.save() self.food_chicken = FoodFactory(food_type="Chicken") self.food_chicken.save() #self.event.food_types.add(self.food) #self.event.food_types.add(self.food_chicken) def test_post_successfully_created_event(self): self.client.login(username="******", password="******") event = { 'name': 'Rock and Roll', 'category': 'BBQ', 'event_date': str(date.today()), 'chicken': 'on', 'pork': 'on' } response = self.client.post('/events/item/', json.dumps(event), content_type='application/json') self.assertEqual(response.status_code, 302) def test_post_not_logged_in_created_event(self): event = { 'name': 'Rock and Roll', 'category': 'BBQ', 'event_date': str(date.today()), 'chicken': 'on', 'pork': 'on' } response = self.client.post('/events/item/', json.dumps(event), content_type='application/json') self.assertEqual(response.status_code, 401) def test_post_create_already_created_event(self): self.client.login(username="******", password="******") event = { 'name': self.event.name, 'category': self.event.category, 'event_date': str(self.event.event_date), 'chicken': 'on', 'pork': 'on' } response = self.client.post('/events/item/', json.dumps(event), content_type='application/json') self.assertEqual(response.status_code, 400) def test_post_wrong_date(self): self.client.login(username="******", password="******") event = { 'name': 'Rock and Roll', 'category': 'BBQ', 'event_date': str(date.today() - timedelta(days=1)), 'chicken': 'on', 'pork': 'on' } response = self.client.post('/events/item/', json.dumps(event), content_type='application/json') self.assertEqual(response.status_code, 400) def test_post_wrong_name(self): self.client.login(username="******", password="******") event = { 'name': '', 'category': 'BBQ', 'event_date': str(date.today()), 'chicken': 'on', 'pork': 'on' } response = self.client.post('/events/item/', json.dumps(event), content_type='application/json') self.assertEqual(response.status_code, 400) def test_post_empty_category(self): self.client.login(username="******", password="******") event = { 'name': 'Rock and Roll', 'category': None, 'event_date': str(date.today()), 'chicken': 'on', 'pork': 'on' } response = self.client.post('/events/item/', json.dumps(event), content_type='application/json') self.assertEqual(response.status_code, 400) def test_post_empty_food_types(self): self.client.login(username="******", password="******") event = { 'name': 'Rock and Roll', 'category': 'BBQ', 'event_date': str(date.today()), 'noodels': 'on' } response = self.client.post('/events/item/', json.dumps(event), content_type='application/json') self.assertEqual(response.status_code, 400) def test_post_empty_food_types(self): self.client.login(username="******", password="******") event = { 'name': 'Rock and Roll', 'category': 'BBQ', 'event_date': str(date.today()), } response = self.client.post('/events/item/', json.dumps(event), content_type='application/json') self.assertEqual(response.status_code, 400) def test_post_empty_date(self): self.client.login(username="******", password="******") event = { 'name': 'Rock and Roll', 'category': 'BBQ', 'event_date': None, 'food_types': [self.food.id, self.food_chicken.id] } response = self.client.post('/events/item/', json.dumps(event), content_type='application/json') self.assertEqual(response.status_code, 400)