def test_should_return_user_todos_if_user_is_admin(self): admin_user = UserFactory(is_admin=True, is_email_confirmed=True) simple_user = UserFactory(is_email_confirmed=True) todos_count = 20 ToDoFactory.create_many(simple_user, count=todos_count) path = reverse('app:users_todos', args=[simple_user.id]) headers = {"HTTP_AUTHORIZATION": TOKEN_PREFIX + ' ' + admin_user.token} response = self.client.get(path, **headers) body = response.json() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(body), todos_count)
def test_should_return_all_logged_user_todos(self): user = UserFactory() user.is_email_confirmed = True user.save() todos = ToDoFactory.create_many(user) path = reverse('app:todos_list_create') headers = {"HTTP_AUTHORIZATION": TOKEN_PREFIX + ' ' + user.token} response = self.client.get(path, **headers) body = response.json() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(body), len(todos))
def test_send_monthly_invoice(self): """Method to test monthly invoices""" with self.app.test_request_context(): TicketFeesFactory(service_fee=10.23, maximum_fee=11) test_event = EventFactoryBasic(state='published') test_user = UserFactory() test_order = OrderFactory(status='completed') test_order.completed_at = datetime.datetime.now( ) - datetime.timedelta(days=30) test_order.amount = 100 test_order.event = test_event test_ticket_holder = AttendeeFactory() test_ticket_holder.event = test_event test_ticket_holder.order = test_order test_event.owner = test_user db.session.commit() send_monthly_event_invoice() event_invoice = EventInvoice.query.get(1) self.assertEqual(event_invoice.amount, 10.23)
def test_should_return_only_not_overdue_tasks_if_i_add_actual_true_to_query_param( self): finished_minute_before = timezone.now() - timedelta(minutes=1) user = UserFactory() user.is_email_confirmed = True user.save() ToDoFactory(user=user, finish_at=finished_minute_before) path = reverse('app:todos_list_create') + '?actual=True' headers = {"HTTP_AUTHORIZATION": TOKEN_PREFIX + ' ' + user.token} response = self.client.get(path, **headers) body = response.json() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(body), 0)
def test_should_return_only_is_not_done_tasks_if_i_add_actual_true_to_query_param( self): todos_count = 20 user = UserFactory() user.is_email_confirmed = True user.save() ToDoFactory.create_many(user, is_done=True, count=todos_count) path = reverse('app:todos_list_create', actual=True) headers = {"HTTP_AUTHORIZATION": TOKEN_PREFIX + ' ' + user.token} response = self.client.get(path, **headers) body = response.json() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(body), 0)
def test_should_order_by_finish_at(self): todos_count = 20 user = UserFactory() user.is_email_confirmed = True user.save() todos = ToDoFactory.create_many(user, count=todos_count) todos = todos.order_by('finish_at') path = reverse('app:todos_list_create') headers = {"HTTP_AUTHORIZATION": TOKEN_PREFIX + ' ' + user.token} response = self.client.get(path, **headers) body = response.json() self.assertEqual(response.status_code, status.HTTP_200_OK) for i in range(todos_count): self.equal_to_do_dicts(todos[i].__dict__, body[i])
def test_should_return_current_user(self): user = UserFactory() path = reverse('app:users_current') headers = {"HTTP_AUTHORIZATION": TOKEN_PREFIX + ' ' + user.token} response = self.client.get(path, **headers) body = response.json() self.assertEqual(response.status_code, status.HTTP_200_OK) self.equal_user_dicts(body, user.__dict__)
def test_should_raise_error_at_create_todo_if_user_email_is_not_confirmed( self): user = UserFactory(is_email_confirmed=False) test_data_set = ToDoTestDataSet(user.id) path = reverse('app:todos_list_create') headers = {"HTTP_AUTHORIZATION": TOKEN_PREFIX + ' ' + user.token} data = test_data_set.__dict__ response = self.client.post(path, data=data, **headers) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_should_raise_error_at_create_todo_if_user_unauthorized(self): user = UserFactory(is_email_confirmed=True) test_data_set = ToDoTestDataSet(user.id) data = test_data_set.__dict__ invalid_token = 'efierngilewrglewgbh' path = reverse('app:todos_list_create') + '?actual=True' headers = {"HTTP_AUTHORIZATION": TOKEN_PREFIX + ' ' + invalid_token} response = self.client.post(path, data, **headers) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def setUp(self): self.app = Setup.create_app() with self.app.test_request_context(): user = UserFactory() save_to_db(user) event = EventFactoryBasic() event.user_id = user.id save_to_db(event) # Authenticate User self.auth = {'Authorization': "JWT " + create_access_token(user.id, fresh=True)}
def test_should_create_todo_if_body_is_valid(self): user = UserFactory(is_email_confirmed=True) test_data_set = ToDoTestDataSet(user.id) path = reverse('app:todos_list_create') headers = {"HTTP_AUTHORIZATION": TOKEN_PREFIX + ' ' + user.token} data = test_data_set.__dict__ response = self.client.post(path, data=data, **headers) body = response.json() self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.equal_to_do_dicts(data, body)
def test_jwt_authenticate(self): with app.test_request_context(): user = UserFactory() db.session.add(user) db.session.commit() # Valid Authentication authenticated_user = jwt_authenticate(user.email, 'password') self.assertEqual(authenticated_user.email, user.email) # Invalid Authentication wrong_credential_user = jwt_authenticate(user.email, 'wrong_password') self.assertIsNone(wrong_credential_user)
def test_should_confirmed_email_if_token_is_valid(self): password = '******' user = UserFactory() user.is_email_confirmed = False user.set_password(password) user.save() path = reverse('app:users_confirm_email', args=[user.token]) response = self.client.get(path) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def test_jwt_authenticate(self): """Method to test jwt authentication""" with self.app.test_request_context(): user = UserFactory() save_to_db(user) # Valid Authentication authenticated_user = jwt_authenticate(user.email, 'password') self.assertEqual(authenticated_user.email, user.email) # Invalid Authentication wrong_credential_user = jwt_authenticate(user.email, 'wrong_password') self.assertIsNone(wrong_credential_user)
def test_should_raise_error_to_get_users_if_user_is_not_admin(self): user = UserFactory() user.is_admin = False user.is_email_confirmed = True user.is_staff = True user.save() path = reverse('app:users_list') headers = {"HTTP_AUTHORIZATION": TOKEN_PREFIX + ' ' + user.token} response = self.client.get(path, **headers) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def setUp(self): self.app = Setup.create_app() with app.test_request_context(): user = UserFactory() save_to_db(user) event = EventFactoryBasic() event.user_id = user.id save_to_db(event) # Authenticate User self.auth = { 'Authorization': "JWT " + str(_default_jwt_encode_handler(user), 'utf-8') }
def setUp(self): self.app = Setup.create_app() with app.test_request_context(): user = UserFactory() db.session.add(user) db.session.commit() event = EventFactoryBasic() event.user_id = user.id db.session.add(event) db.session.commit() # Authenticate User self.auth = { 'Authorization': "JWT " + _default_jwt_encode_handler(user) }
def test_get_identity(self): with app.test_request_context(): user = UserFactory() db.session.add(user) db.session.commit() event = EventFactoryBasic() event.user_id = user.id db.session.add(event) db.session.commit() # Authenticate User self.auth = {'Authorization': "JWT " + _default_jwt_encode_handler(user)} with app.test_request_context(headers=self.auth): self.assertEquals(get_identity().id, user.id)
def test_get_identity(self): """Method to test identity of authenticated user""" with self.app.test_request_context(): user = UserFactory() save_to_db(user) event = EventFactoryBasic() event.user_id = user.id save_to_db(event) # Authenticate User self.auth = {'Authorization': "JWT " + create_access_token(user.id, fresh=True)} with self.app.test_request_context(headers=self.auth): self.assertEquals(get_identity().id, user.id)
def test_should_return_all_users_except_him_if_user_is_admin(self): user = UserFactory() user.is_admin = True user.is_email_confirmed = True user.is_staff = True user.save() other_users = UserFactory.create_many(10) path = reverse('app:users_list') headers = {"HTTP_AUTHORIZATION": TOKEN_PREFIX + ' ' + user.token} response = self.client.get(path, **headers) body = response.json() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(body), len(other_users)) for i in range(10): self.equal_user_dicts(body[i], other_users[i].__dict__)
def test_should_raise_error_if_user_email_is_not_confirmed(self): password = '******' user = UserFactory() user.is_email_confirmed = False user.set_password(password) user.save() path = reverse('app:users_login') data = {"username": user.username, "password": password} response = self.client.post(path, data=data, content_type='application/json') body = response.json() self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(body['error'][0], 'user.email.notConfirmed')
def test_should_auth_user_if_login_and_password_is_correct(self): password = '******' user = UserFactory() user.is_email_confirmed = True user.set_password(password) user.save() path = reverse('app:users_login') data = {"username": user.username, "password": password} response = self.client.post(path, data=data, content_type='application/json') body = response.json() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(body['username'], user.username)
def test_should_update_todo_if_data_is_valid(self): user = UserFactory(is_email_confirmed=True) todo = ToDoFactory(user=user) test_body = ToDoTestDataSet(**todo.__dict__) data = test_body.__dict__ data['name'] = 'new Name' path = reverse('app:todos_retrieve_update', args=[todo.id]) headers = {"HTTP_AUTHORIZATION": TOKEN_PREFIX + ' ' + user.token} response = self.client.put(path, data=data, **headers, content_type='application/json') body = response.json() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(body['name'], 'new Name')
def test_get_identity(self): """Method to test identity of authenticated user""" with app.test_request_context(): user = UserFactory() save_to_db(user) event = EventFactoryBasic() event.user_id = user.id save_to_db(event) # Authenticate User self.auth = { 'Authorization': "JWT " + str(_default_jwt_encode_handler(user), 'utf-8') } with app.test_request_context(headers=self.auth): self.assertEquals(get_identity().id, user.id)
def test_export_with_multiple_sessions(self): """Test to check event with sessions in pentabarfxml format""" with app.test_request_context(): keynote = SessionFactory( title='Keynote', starts_at=datetime(2019, 10, 15, 10, 25, 46), ends_at=datetime(2019, 10, 15, 11, 10, 46), track__name='Amazing Track', microlocation__name='Great Hall', event__name='Awesome Conference', event__starts_at=datetime(2019, 10, 15), event__ends_at=datetime(2019, 10, 16, 13, 30, 00)) UserFactory() mario = SpeakerFactoryBase.build(name='Mario Behling', user_id=1) keynote.speakers = [ mario, SpeakerFactoryBase.build(name='Hong Phuc Dang', user_id=1) ] SessionFactoryBase(title='Hot Session', starts_at=datetime(2019, 10, 15, 11, 30, 00), ends_at=datetime(2019, 10, 15, 12, 00, 54)) future_session = SessionFactoryBase( title='Future Session', starts_at=datetime(2019, 10, 16, 9, 15, 30), ends_at=datetime(2019, 10, 16, 10, 30, 45)) future_session.speakers = [ SpeakerFactoryBase.build(name='Pranav Mistry', user_id=1) ] MicrolocationFactoryBase(name='Assembly Hall') end_session = SessionFactoryBase( title='Bye Bye Session', starts_at=datetime(2019, 10, 16, 11, 30, 20), ends_at=datetime(2019, 10, 16, 13, 00, 30), microlocation_id=2) end_session.speakers = [mario] db.session.commit() pentabarf_export = PentabarfExporter() pentabarf_string = pentabarf_export.export(keynote.event.id) pentabarf_original = fromstring(pentabarf_string) self.assertEqual( pentabarf_original.find('conference/title').text, "Awesome Conference") self.assertEqual( pentabarf_original.find('conference/start').text, '2019-10-15') self.assertEqual( pentabarf_original.find('conference/end').text, '2019-10-16') self.assertEqual( pentabarf_original.find('conference/days').text, '1') self.assertEqual( pentabarf_original.find('day/room').attrib['name'], 'Great Hall') self.assertEqual( pentabarf_original.find('day/room/event/title').text, 'Keynote') self.assertEqual( pentabarf_original.find('day/room/event/track').text, 'Amazing Track') self.assertEqual( pentabarf_original.find('day/room/event/start').text, '10:25') self.assertEqual( pentabarf_original.find('day/room/event/duration').text, '00:45') self.assertEqual( pentabarf_original.find( 'day/room/event/persons/person[@id="2"]').text, 'Hong Phuc Dang') self.assertEqual( len( pentabarf_original.find( 'day/room/event/persons').getchildren()), 2) self.assertEqual( pentabarf_original.find('day/room/event[2]/title').text, 'Hot Session') self.assertEqual( pentabarf_original.find('day[2]/room/event/title').text, 'Future Session') self.assertEqual( pentabarf_original.find( 'day[2]/room/event/persons/person').text, 'Pranav Mistry') self.assertEqual( pentabarf_original.find('day[2]/room[2]').attrib['name'], 'Assembly Hall') self.assertEqual( pentabarf_original.find('day[2]/room[2]/event/title').text, 'Bye Bye Session') self.assertEqual( pentabarf_original.find('day[2]/room[2]/event/duration').text, '01:30') self.assertEqual( pentabarf_original.find( 'day[2]/room[2]/event/persons/person').text, 'Mario Behling')