def test_can_update_administrated_community_categories(self): """ should be able to update the administrated community categories and return 200 """ user = make_user() headers = make_authentication_headers_for_user(user) community = make_community(creator=user) new_categories = [] for i in range(settings.COMMUNITY_CATEGORIES_MIN_AMOUNT, settings.COMMUNITY_CATEGORIES_MAX_AMOUNT): category = make_category() new_categories.append(category) data = { 'categories': ','.join(map(str, [category.name for category in new_categories])) } url = self._get_url(community_name=community.name) response = self.client.patch(url, data, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) community.refresh_from_db() categories = community.categories.all() categories_ids = [category.pk for category in categories] self.assertEqual(len(categories), len(new_categories)) for new_category in new_categories: self.assertIn(new_category.pk, categories_ids)
def test_cannot_create_community_without_credentials(self): """ should NOT be able to create a community without providing credentials and return 400 """ community_name = fake.user_name() community_title = fake.name_male() community_color = fake.hex_color() community_categories = [] community_type = 'T' for i in range(0, settings.COMMUNITY_CATEGORIES_MAX_AMOUNT): category = make_category() community_categories.append(category.name) data = { 'name': community_name, 'type': community_type, 'title': community_title, 'color': community_color, 'categories': community_categories, } url = self._get_url() response = self.client.put(url, data, format='multipart') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) self.assertFalse( Community.objects.filter(name=community_name, title=community_title, color=community_color, type=community_type).count() == 1)
def test_can_create_community_without_optional_params(self): """ should be able to create a community without providing the optional arguments and return 201 """ user = make_user() headers = make_authentication_headers_for_user(user) community_name = fake.user_name() community_title = fake.name_male() community_color = fake.hex_color() community_categories = [] community_type = 'T' for i in range(0, settings.COMMUNITY_CATEGORIES_MAX_AMOUNT): category = make_category() community_categories.append(category.name) data = { 'name': community_name, 'type': community_type, 'title': community_title, 'color': community_color, 'categories': community_categories, } url = self._get_url() response = self.client.put(url, data, **headers, format='multipart') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertTrue( Community.objects.filter(name=community_name, title=community_title, color=community_color, type=community_type).count() == 1)
def test_cannot_create_a_category_with_less_than_minimal_categories(self): """ should NOT be able to create a community with a less that minimal category amount and return 400 """ user = make_user() headers = make_authentication_headers_for_user(user) community_name = fake.user_name() community_title = fake.name_male() community_color = fake.hex_color() community_categories = [] community_type = 'T' for i in range(0, settings.COMMUNITY_CATEGORIES_MIN_AMOUNT - 1): category = make_category() community_categories.append(category.name) data = { 'name': community_name, 'type': community_type, 'title': community_title, 'color': community_color, 'categories': community_categories } url = self._get_url() response = self.client.put(url, data, **headers, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertFalse(Community.objects.filter(name=community_name).exists())
def test_create_community_should_not_make_creator_mod(self): """ should NOT make the community creator a moderator when creating a new community """ user = make_user() headers = make_authentication_headers_for_user(user) community_name = fake.user_name() community_title = fake.name_male() community_color = fake.hex_color() community_categories = [] community_type = 'T' for i in range(0, settings.COMMUNITY_CATEGORIES_MAX_AMOUNT): category = make_category() community_categories.append(category.name) data = { 'name': community_name, 'type': community_type, 'title': community_title, 'color': community_color, 'categories': community_categories, } url = self._get_url() self.client.put(url, data, **headers, format='multipart') self.assertFalse(user.is_moderator_of_community_with_name(community_name=community_name))
def test_can_retrieve_categories(self): """ should be able to retrieve all categories and return 200 """ user = make_user() amount_of_categories = 5 categories_ids = [] for i in range(0, amount_of_categories): category = make_category() categories_ids.append(category.pk) url = self._get_url() headers = make_authentication_headers_for_user(user) response = self.client.get(url, **headers) self.assertEqual(response.status_code, status.HTTP_200_OK) response_categories = json.loads(response.content) self.assertEqual(len(response_categories), len(categories_ids)) for response_category in response_categories: response_category_id = response_category.get('id') self.assertIn(response_category_id, categories_ids)
def test_create_public_community(self): """ should be able to create a public community and return 201 """ user = make_user() headers = make_authentication_headers_for_user(user) community_name = fake.user_name() community_title = fake.name_male() community_description = fake.text( max_nb_chars=settings.COMMUNITY_DESCRIPTION_MAX_LENGTH) community_rules = fake.text( max_nb_chars=settings.COMMUNITY_RULES_MAX_LENGTH) community_user_adjective = make_community_user_adjective() community_users_adjective = make_community_users_adjective() community_color = fake.hex_color() community_categories = [] community_type = 'P' for i in range(0, settings.COMMUNITY_CATEGORIES_MAX_AMOUNT): category = make_category() community_categories.append(category.name) data = { 'name': community_name, 'type': community_type, 'title': community_title, 'description': community_description, 'rules': community_rules, 'user_adjective': community_user_adjective, 'users_adjective': community_users_adjective, 'color': community_color, 'categories': community_categories } url = self._get_url() response = self.client.put(url, data, **headers, format='multipart') self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertTrue( Community.objects.filter(name=community_name, title=community_title, description=community_description, rules=community_rules, user_adjective=community_user_adjective, users_adjective=community_users_adjective, color=community_color, type=community_type).count() == 1)
def test_create_community_with_cover(self): """ should be able to create a community with a cover and return 201 """ user = make_user() headers = make_authentication_headers_for_user(user) community_name = fake.user_name() community_title = fake.name_male() community_description = fake.text( max_nb_chars=settings.COMMUNITY_DESCRIPTION_MAX_LENGTH) community_rules = fake.text( max_nb_chars=settings.COMMUNITY_RULES_MAX_LENGTH) community_user_adjective = fake.word() community_users_adjective = fake.word() community_cover = make_community_cover() community_color = fake.hex_color() community_categories = [] for i in range(0, settings.COMMUNITY_CATEGORIES_MAX_AMOUNT): category = make_category() community_categories.append(category.name) data = { 'name': community_name, 'type': 'P', 'title': community_title, 'description': community_description, 'rules': community_rules, 'user_adjective': community_user_adjective, 'users_adjective': community_users_adjective, 'color': community_color, 'categories': community_categories, 'cover': community_cover } url = self._get_url() response = self.client.put(url, data, **headers, format='multipart') self.assertEqual(response.status_code, status.HTTP_201_CREATED) community = Community.objects.get(name=community_name) self.assertTrue(hasattr(community, 'cover'))
def test_can_create_community_with_categories(self): """ should be able to create a community with categories and return 201 """ user = make_user() headers = make_authentication_headers_for_user(user) community_name = fake.user_name() community_title = fake.name_male() community_color = fake.hex_color() community_categories = [] community_type = 'T' for i in range(0, settings.COMMUNITY_CATEGORIES_MAX_AMOUNT): category = make_category() community_categories.append(category.name) data = { 'name': community_name, 'type': community_type, 'title': community_title, 'color': community_color, 'categories': community_categories } url = self._get_url() response = self.client.put(url, data, **headers, format='json') self.assertEqual(response.status_code, status.HTTP_201_CREATED) community = Community.objects.get(name=community_name) categories = community.categories.all() categories_names = [category.name for category in categories] self.assertEqual(len(categories_names), len(community_categories)) for community_category in community_categories: self.assertIn(community_category, categories_names)
def test_create_public_community_should_enable_member_invites(self): """ should be able to create a public community and automatically emnable member invites and return 201 """ user = make_user() headers = make_authentication_headers_for_user(user) community_name = fake.user_name() community_title = fake.name_male() community_description = fake.text( max_nb_chars=settings.COMMUNITY_DESCRIPTION_MAX_LENGTH) community_rules = fake.text( max_nb_chars=settings.COMMUNITY_RULES_MAX_LENGTH) community_color = fake.hex_color() community_categories = [] community_type = Community.COMMUNITY_TYPE_PUBLIC for i in range(0, settings.COMMUNITY_CATEGORIES_MAX_AMOUNT): category = make_category() community_categories.append(category.name) data = { 'name': community_name, 'type': community_type, 'title': community_title, 'description': community_description, 'rules': community_rules, 'color': community_color, 'categories': community_categories } url = self._get_url() response = self.client.put(url, data, **headers, format='multipart') self.assertEqual(response.status_code, status.HTTP_201_CREATED) community = Community.objects.get(name=community_name) self.assertTrue(community.invites_enabled)