<commit_before>import json

from django.test import TestCase
from django.test.client import Client
from django.shortcuts import resolve_url as r
from django.contrib.auth.models import User

from myideas.core.models import Ideas


class LikeApiTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.username = 'diego'
        self.email = '<EMAIL>'
        self.password = '<PASSWORD>'
        user = User.objects.create_user(
            self.username, self.email, self.password
        )
        self.idea = Ideas.objects.create(
            user=user, title='test app'
        )

    def api_signin_and_get(self):
        self.login = self.client.login(
            username=self.username, password=<PASSWORD>
        )
        self.response = self.client.get(r(self.idea.get_api_like_url()))

    def test_get(self):
        """GET 'Ideas like api' must return status code 200"""
        self.api_signin_and_get()
        self.assertEqual(200, self.response.status_code)

    def test_api_status_likes(self):
        self.api_signin_and_get()
        self.assertTrue(self.response)

    def test_api_likes_count(self):
        self.api_signin_and_get()
        self.assertEqual(1, self.idea.likes.count())
        self.api_signin_and_get()
        self.assertEqual(0, self.idea.likes.count())

    def test_content(self):
        self.api_signin_and_get()
        response = json.loads(self.response.content.decode('utf-8'))
        self.assertEqual(True, response['updated'])
        self.assertEqual(True, response['liked'])
        self.assertIn('updated', response)
        self.assertIn('liked', response)

    def test_access_forbidden(self):
        """GET page not logged in must return status code 403"""
        self.response = self.client.get(r(self.idea.get_api_like_url()))
        self.assertEqual(403, self.response.status_code)<commit_msg>refactor test api<commit_after>import json

from django.test import TestCase
from django.test.client import Client
from django.shortcuts import resolve_url as r
from django.contrib.auth.models import User

from rest_framework import status

from myideas.core.models import Ideas


class LikeApiTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.username = 'diego'
        self.email = '<EMAIL>'
        self.password = '<PASSWORD>'
        user = User.objects.create_user(
            self.username, self.email, self.password
        )
        self.idea = Ideas.objects.create(
            user=user, title='test app'
        )

    def api_signin_and_get(self):
        self.login = self.client.login(
            username=self.username, password=<PASSWORD>
        )
        self.response = self.client.get(r(self.idea.get_api_like_url()))
        self.get_json = json.loads(self.response.content.decode('utf-8'))

    def test_get(self):
        """GET 'Ideas like api' must return status code 200"""
        self.api_signin_and_get()
        self.assertEqual(200, self.response.status_code)

    def test_api_likes_count(self):
        self.api_signin_and_get()
        self.assertEqual(1, self.idea.likes.count())
        self.api_signin_and_get()
        self.assertEqual(0, self.idea.likes.count())

    def test_content(self):
        self.api_signin_and_get()
        self.assertEqual(self.get_json, {"liked": True, "updated": True})
        self.api_signin_and_get()
        self.assertEqual(self.get_json, {"liked": False, "updated": True})

    def test_htm(self):
        self.api_signin_and_get()
        self.assertIn('updated', self.get_json)
        self.assertIn('liked', self.get_json)

    def test_access_forbidden(self):
        """GET page not logged in must return status code 403"""
        self.response = self.client.get(r(self.idea.get_api_like_url()))
        self.assertEqual(403, self.response.status_code)
{'fraction_non_alphanumeric': 0.1158140742664243, 'fraction_numerical': 0.007790184367696702, 'mean_word_length': 2.844311377245509, 'pattern_counts': {'":': 4, '<': 9, '<?xml version=': 0, '>': 9, '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': '2537917', 'n_tokens_mistral': 1214, 'n_tokens_neox': 1169, 'n_words': 229}