<commit_before>from django.urls import reverse
from django.contrib.auth.models import User

from rest_framework import status
from rest_framework.test import APITestCase
from rest_framework.authtoken.models import Token

from users.models import Author
from .models import Story, StoryLine


class StoriesViewSetTests(APITestCase):
    def setUp(self):
        self.list_view_name = 'stories:story-list'
        self.detail_view_name = 'stories:story-detail'

        self.user = User.objects.create(
            username='Litelight',
            password='<PASSWORD>',
            first_name='Kndrck',
            last_name='Lmr'
        )
        self.author = Author.objects.create(user=self.user)
        self.story = Story.objects.create(title='Test!',author=self.author)

        self.request_body = {'title': 'Drama goes down.'}
        self.http_401_message = 'Authentication credentials were not provided.'
        self.http_403_message = 'You do not have permission to perform this action.'
        self.http_404_message = 'Not found.'

    def test_story_list_with_unauthorized_user(self):
        response = self.client.get(reverse(self.list_view_name))

        self.assertEqual(response.data['detail'], self.http_401_message)
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_story_list_with_authorized_user(self):
        self.client.force_authenticate(user=self.user)

        response = self.client.get(reverse(self.list_view_name))

        self.assertEqual(self.story.title, response.data[0]['title'])
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_story_detail_with_unauthorized_user(self):
        response = self.client.get(reverse(self.detail_view_name, kwargs={'pk': self.story.id}))

        self.assertEqual(response.data['detail'], self.http_401_message)
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_story_detail_with_authorized_user(self):
        self.client.force_authenticate(user=self.user)

        response = self.client.get(reverse(self.detail_view_name, kwargs={'pk': self.story.id}))

        self.assertEqual(self.story.title, response.data['title'])
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_story_detail_with_invalid_id(self):
        self.client.force_authenticate(user=self.user)
        
        response = self.client.get(reverse(self.detail_view_name, kwargs={'pk': self.story.id + 1}))

        self.assertEqual(response.data['detail'], self.http_404_message)
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_story_creation_with_unauthorized_user(self):
        response = self.client.post(reverse(self.list_view_name), self.request_body, format='json')

        self.assertEqual(response.data['detail'], self.http_401_message)
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_story_creation_with_authorized_user(self):
        self.client.force_authenticate(user=self.user)

        response = self.client.post(reverse(self.list_view_name), self.request_body, format='json')

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    def test_story_deletion_with_unauthorized_user(self):
        response = self.client.delete(reverse(self.detail_view_name, kwargs={'pk': self.story.id}))

        self.assertEqual(response.data['detail'], self.http_401_message)
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_story_deletion_of_another_user(self):
        self.client.force_authenticate(user=self.user)

        self.new_user = User.objects.create(username='joey', password='<PASSWORD>')
        self.story.author = Author.objects.create(user=self.new_user)
        self.story.save()

        response = self.client.delete(reverse(self.detail_view_name, kwargs={'pk': self.story.id}))

        self.assertEqual(response.data['detail'], self.http_403_message)
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_story_deletion_with_valid_user(self):
        self.client.force_authenticate(user=self.user)

        response = self.client.delete(reverse(self.detail_view_name, kwargs={'pk': self.story.id}))

        self.assertEqual(response.data['message'], 'Story successfully deleted.')
        self.assertEqual(response.status_code, status.HTTP_200_OK)<commit_msg>Added category story list test cases<commit_after>from django.urls import reverse
from django.contrib.auth.models import User

from rest_framework import status
from rest_framework.test import APITestCase
from rest_framework.authtoken.models import Token

from users.models import Author
from .models import Story, StoryLine


HTTP_MESSAGES = {
    401: 'Authentication credentials were not provided.',
    403: 'You do not have permission to perform this action.',
    404: 'Not found.'
}


class StoriesViewSetTests(APITestCase):
    def setUp(self):
        self.list_view_name = 'stories:story-list'
        self.detail_view_name = 'stories:story-detail'

        self.user = User.objects.create(
            username='Litelight',
            password='<PASSWORD>',
            first_name='Kndrck',
            last_name='Lmr'
        )
        self.author = Author.objects.create(user=self.user)
        self.story = Story.objects.create(title='Test!', author=self.author)

        self.request_body = {'title': 'Drama goes down.'}

    def test_story_list_with_unauthorized_user(self):
        response = self.client.get(reverse(self.list_view_name))

        self.assertEqual(response.data['detail'], HTTP_MESSAGES[response.status_code])
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_story_list_with_authorized_user(self):
        self.client.force_authenticate(user=self.user)

        response = self.client.get(reverse(self.list_view_name))

        self.assertEqual(self.story.title, response.data[0]['title'])
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_story_detail_with_unauthorized_user(self):
        response = self.client.get(reverse(self.detail_view_name, kwargs={'pk': self.story.id}))

        self.assertEqual(response.data['detail'], HTTP_MESSAGES[response.status_code])
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_story_detail_with_authorized_user(self):
        self.client.force_authenticate(user=self.user)

        response = self.client.get(reverse(self.detail_view_name, kwargs={'pk': self.story.id}))

        self.assertEqual(self.story.title, response.data['title'])
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_story_detail_with_invalid_id(self):
        self.client.force_authenticate(user=self.user)
        
        response = self.client.get(reverse(self.detail_view_name, kwargs={'pk': self.story.id + 1}))

        self.assertEqual(response.data['detail'], HTTP_MESSAGES[response.status_code])
        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    def test_story_creation_with_unauthorized_user(self):
        response = self.client.post(reverse(self.list_view_name), self.request_body, format='json')

        self.assertEqual(response.data['detail'], HTTP_MESSAGES[response.status_code])
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_story_creation_with_authorized_user(self):
        self.client.force_authenticate(user=self.user)

        response = self.client.post(reverse(self.list_view_name), self.request_body, format='json')

        self.assertEqual(response.status_code, status.HTTP_201_CREATED)

    def test_story_deletion_with_unauthorized_user(self):
        response = self.client.delete(reverse(self.detail_view_name, kwargs={'pk': self.story.id}))

        self.assertEqual(response.data['detail'], HTTP_MESSAGES[response.status_code])
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_story_deletion_of_another_user(self):
        self.client.force_authenticate(user=self.user)

        self.new_user = User.objects.create(username='joey', password='<PASSWORD>')
        self.story.author = Author.objects.create(user=self.new_user)
        self.story.save()

        response = self.client.delete(reverse(self.detail_view_name, kwargs={'pk': self.story.id}))

        self.assertEqual(response.data['detail'], HTTP_MESSAGES[response.status_code])
        self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

    def test_story_deletion_with_valid_user(self):
        self.client.force_authenticate(user=self.user)

        response = self.client.delete(reverse(self.detail_view_name, kwargs={'pk': self.story.id}))

        self.assertEqual(response.data['message'], 'Story successfully deleted.')
        self.assertEqual(response.status_code, status.HTTP_200_OK)

class CategoryStoryListViewTests(APITestCase):
    def setUp(self):
        self.view_name = 'stories:category-list'

        self.user = User.objects.create(
            username='Litelight',
            password='<PASSWORD>',
            first_name='Kndrck',
            last_name='Lmr'
        )
        self.author = Author.objects.create(user=self.user)
        self.story1 = Story.objects.create(title='Test!', author=self.author)
        self.story2 = Story.objects.create(title='Feel the buzz!', author=self.author)

    def test_category_list_with_unauthorized_user(self):
        response = self.client.get(reverse(self.view_name, kwargs={'category': 'test'}))

        self.assertEqual(response.data['detail'], HTTP_MESSAGES[response.status_code])
        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

    def test_category_list_with_invalid_category(self):
        self.client.force_authenticate(user=self.user)

        response = self.client.get(reverse(self.view_name, kwargs={'category': 'test'}))

        self.assertEqual(response.data['message'], 'Invalid category.')
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

    def test_personal_category_list(self):
        self.client.force_authenticate(user=self.user)

        response = self.client.get(reverse(self.view_name, kwargs={'category': 'personal'}))

        self.assertEqual(response.data[0]['title'], self.story2.title)
        self.assertEqual(response.data[1]['title'], self.story1.title)
        self.assertEqual(response.status_code, status.HTTP_200_OK)

    def test_trending_category_list(self):
        self.client.force_authenticate(user=self.user)

        self.story1.num_vote_up += 3
        self.story2.num_vote_up += 1
        self.story1.save()
        self.story2.save()
        response = self.client.get(reverse(self.view_name, kwargs={'category': 'trending'}))

        self.assertEqual(response.data[0]['title'], self.story1.title)
        self.assertEqual(response.data[1]['title'], self.story2.title)
        self.assertEqual(response.status_code, status.HTTP_200_OK)
{'fraction_non_alphanumeric': 0.11642167914149157, 'fraction_numerical': 0.011542970511317523, 'mean_word_length': 3.9865107913669067, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '607764', 'n_tokens_mistral': 3461, 'n_tokens_neox': 3317, 'n_words': 442}