def test_permissions(self): another_user = UserFactory() password = fuzzy.FuzzyText().fuzz() another_user.set_password(password) another_user.save() response = self.client.delete(self._get_url(another_user.id), HTTP_AUTHORIZATION=self.access_header) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_failed_auth(self): user = UserFactory() user.set_password(fuzzy.FuzzyText().fuzz()) user.save() response = self.client.post(reverse('api_auth_login_api_view'), data={ 'username': user.username, 'password': fuzzy.FuzzyText().fuzz() }) self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
def test_is_teacher_permissions(self): user = UserFactory(is_teacher=True) password = fuzzy.FuzzyText().fuzz() user.set_password(password) user.save() key = self.client.post(reverse('api_auth_login_api_view'), data={ 'username': user.username, 'password': password }).json()['key'] queue = QueueFactory(creator=self.user) for token in (self._get_auth_header(key), self.access_header): res = self.client.put( reverse( 'api_queue_add_member_api_view', kwargs={'pk': str(queue.id)} ), data={'userId': str(user.id)}, content_type='application/json', HTTP_AUTHORIZATION=token ) self.assertEqual(res.status_code, status.HTTP_403_FORBIDDEN) user_1, user_2 = UserFactory(), UserFactory() queue.push_member(str(user_1.id)) queue.push_member(str(user_2.id)) queue.save() queue.refresh_from_db() self.assertEqual(queue.members, [str(user_2.id), str(user_1.id)]) res = self.client.put( reverse('api_queue_skip_turn_api_view', kwargs={'pk': str(queue.id)}), data={ 'userId': str(user_1.id) }, content_type='application/json', HTTP_AUTHORIZATION=self._get_auth_header(key) ) self.assertEqual(res.status_code, status.HTTP_200_OK) queue.refresh_from_db() self.assertEqual(queue.members, [str(user_1.id), str(user_2.id)]) res = self.client.put( reverse( 'api_queue_remove_member_api_view', kwargs={'pk': str(queue.id)} ), data={'userId': str(user_2.id)}, content_type='application/json', HTTP_AUTHORIZATION=self._get_auth_header(key) ) self.assertEqual(res.status_code, status.HTTP_200_OK) queue.refresh_from_db() self.assertEqual(queue.members, [str(user_1.id)])
class AuthMixin: def setUp(self) -> None: self.user = UserFactory() self.user_password = fuzzy.FuzzyText().fuzz() self.user.set_password(self.user_password) self.user.save() key = self.client.post(reverse('api_auth_login_api_view'), data={ 'username': self.user.username, 'password': self.user_password }).json()['key'] self.access_header = f"Token {key}"
def test_success_auth(self): user = UserFactory() password = fuzzy.FuzzyText().fuzz() user.set_password(password) user.save() response = self.client.post(reverse('api_auth_login_api_view'), data={ 'username': user.username, 'password': password }) self.assertEqual(response.status_code, status.HTTP_200_OK) res = self.client.get(reverse('api_queue_list_create_api_view'), HTTP_AUTHORIZATION=self._get_auth_header( response.json()['key'])) self.assertEqual(res.status_code, status.HTTP_200_OK)
def test_logout(self): user = UserFactory() password = fuzzy.FuzzyText().fuzz() user.set_password(password) user.save() key = self.client.post(reverse('api_auth_login_api_view'), data={ 'username': user.username, 'password': password }).json()['key'] response = self.client.post( reverse('api_auth_logout_api_view'), HTTP_AUTHORIZATION=self._get_auth_header(key)) self.assertEqual(response.status_code, status.HTTP_205_RESET_CONTENT) response = self.client.get(reverse('api_queue_list_create_api_view')) self.assertEquals(response.status_code, status.HTTP_401_UNAUTHORIZED)
def handle(self, *args, **options): if not settings.DUMMY_DATA_ENABLED: print('Dummy data generation is not enabled') return # print(settings.DATABASES) # print(settings.DATABASES['default']) # print(settings.DATABASES['default']['USER']) # return with connection.cursor() as cursor: cursor.execute("DROP SCHEMA IF EXISTS public CASCADE;") cursor.execute( "CREATE SCHEMA IF NOT EXISTS public AUTHORIZATION {};".format(settings.DATABASES['default']['USER'])) cursor.execute("GRANT ALL ON SCHEMA public TO postgres;") management.call_command('migrate', verbosity=0, interactive=False) self.ensure_clean_dir('images') self.ensure_clean_dir('original_images') self.ensure_clean_dir('documents') site = wagtail_factories.SiteFactory.create(is_default_site=True) Page.objects.filter(title='Root').delete() category_images = [ ImageField(from_path=os.path.join(settings.BASE_DIR, 'core/static/img/dummy/category0.png')), ImageField(from_path=os.path.join(settings.BASE_DIR, 'core/static/img/dummy/category1.png')), ImageField(from_path=os.path.join(settings.BASE_DIR, 'core/static/img/dummy/category2.png')), ] # category_images = [ # ImageField(from_path=os.path.join(settings.BASE_DIR, 'core/static/img/Mastering-Complexity.svg'), width=60, height=60), # ImageField(from_path=os.path.join(settings.BASE_DIR, 'core/static/img/Growing-as-a-Leader.svg'), width=60, height=60), # ImageField(from_path=os.path.join(settings.BASE_DIR, 'core/static/img/Mastering-Relations.svg'), width=60, height=60), # ] # module_images = [ # ImageField(from_path=os.path.join(settings.BASE_DIR, 'core/static/img/dummy/module_0.png')), # ImageField(from_path=os.path.join(settings.BASE_DIR, 'core/static/img/dummy/module_1.png')), # ImageField(from_path=os.path.join(settings.BASE_DIR, 'core/static/img/dummy/module_2.png')), # ImageField(from_path=os.path.join(settings.BASE_DIR, 'core/static/img/dummy/module_3.png')), # ] module_images = [ ImageField(from_path=os.path.join(settings.BASE_DIR, 'core/static/img/modules/communication_2.png')), ImageField( from_path=os.path.join(settings.BASE_DIR, 'core/static/img/modules/mensch-maschine_3.png')), ImageField( from_path=os.path.join(settings.BASE_DIR, 'core/static/img/modules/compi_2.png')), ImageField( from_path=os.path.join(settings.BASE_DIR, 'core/static/img/modules/moodboard_2.png')), ] u = UserFactory( username='******', is_staff=True, is_superuser=True ) for i in range(0, 4): UserFactory(username='******'.format(i)) module_counter = 0 for idx, category_data in enumerate(data): category = CategoryFactory.create( parent=site.root_page, title=category_data['title'], teaser=category_data['teaser'], icon_component=category_data['icon'], icon__file=category_images[idx % len(category_images)] ) competences = category_data['competences'] for competence in competences: CompetenceFactory.create( title=competence['title'], description=competence['description'], category=category ).save() default_modules = [{} for i in range(0, 2)] modules_data = category_data.get('modules', default_modules) for module_idx, module_data in enumerate(modules_data): module = ModuleFactory.create( parent=category, hero_image__file=module_images[module_idx % len(module_images)], **self.filter_data(module_data, ['units', 'goals']) ) default_units = [{} for i in range(0, random.randint(3, 5))] units_data = module_data.get('units', default_units) for unit_data in units_data: UnitFactory.create(parent=module, **unit_data) default_goals = [{'level': lvl} for lvl in range(0, 3)] goals_data = module_data.get('goals', default_goals) for goal_data in goals_data: GoalFactory.create(module=module, **goal_data) # create user progress UserModuleProgressFactory.create_batch(size=20) all_module_progress = UserModuleProgress.objects.all() for module_progress in all_module_progress: UserUnitProgressFactory.create_batch(size=3, module_progress=module_progress) # create user focus all_users = get_user_model().objects.all() for user in all_users: focus = FocusFactory.create(user=user) CompetenceEntryFactory.create_batch(size=10, focus=focus) # create user goals for users and goals UserGoalFactory.create_batch(size=20) # create user stuff for testing u = UserFactory( username='******', is_staff=True, is_superuser=True ) u.set_password('oRIbsyYmOy') u.save() u = UserFactory( username='******', is_staff=True, is_superuser=True ) u.set_password('ioatQQKvtK') u.save()