<commit_before>from django.test import TestCase, Client
from django.contrib.auth.models import User
import json

class JwtAuthTestCase(TestCase):
    def setUp(self):
        u = User.objects.create_user(username="codert", password="<PASSWORD>")
        u.save()

        self.encoder = json.JSONEncoder()

    def test_token_request(self):
        c1 = Client()
        c2 = Client()
        c3 = Client()
        c4 = Client()
        encoder = self.encoder

        r1 = c1.post('/api/auth/', encoder.encode({ "username": "codert", "pwd": "<PASSWORD>" }), content_type="application/json")
        r2 = c2.post('/api/auth/', encoder.encode({ "username": "codert", "xx": "xxxxxxxx" }), content_type="application/json")
        r3 = c3.post('/api/auth/', encoder.encode({ "username": "codert", "pwd": "<PASSWORD>" }), content_type="application/json")
        r4 = c4.post('/api/auth/', encoder.encode({ "username": "donotexist", "pwd": "<PASSWORD>" }), content_type="application/json")

        self.assertEqual(201, r1.status_code)
        self.assertEqual(r2.json()['detail'], 'Incomplete authentication information')
        self.assertEqual(r3.json()['detail'], 'Incorrect authentication information')
        self.assertEqual(r3.json()['detail'], 'Incorrect authentication information')<commit_msg>[test](jwt_auth): Add user api test<commit_after>from django.test import TestCase, Client
from jwt_auth.models import User 
import json

class UserTestCase(TestCase):
    def setUp(self):
        # used for test unique validation
        User.objects.create_user(username='codert', password="<PASSWORD>", email='<EMAIL>', nickname='哈哈哈')

        self.encoder = json.JSONEncoder()
   
    def test_user_create(self):
        c1 = Client()
        c2 = Client()
        c3 = Client()
        c4 = Client()
        c5 = Client()
        c6 = Client()
        encoder = self.encoder

        # the user info is valid
        r1 = c1.post('/api/user/', encoder.encode({"username": "newuser1", "password":"<PASSWORD>", "nickname": "test1", "email": "<EMAIL>"}), content_type="application/json")        
        # missing required field
        r2 = c2.post('/api/user/', encoder.encode({"username": "newuser2", "password": "<PASSWORD>"}), content_type="application/json")
        # given `username` field is already exist
        r3 = c3.post('/api/user/', encoder.encode({"username": "codert", "password": "<PASSWORD>", "nickname": "test3", "email": "<EMAIL>"}), content_type="application/json")
        # given `email` field is already exist
        r4 = c4.post('/api/user/', encoder.encode({"username": "newuser4", "password": "<PASSWORD>", "nickname": "test4", "email": "<EMAIL>"}), content_type="application/json")
        # given `username` field is already exist but uppercase
        r5 = c5.post('/api/user/', encoder.encode({"username": "CODERT", "password": "<PASSWORD>", "nickname": "test5", "email": "<EMAIL>"}), content_type="application/json")
        # given `email` field is already exist bu uppercase
        r6 = c6.post('/api/user/', encoder.encode({"username": "newuser6", "password": "<PASSWORD>", "nickname": "test6", "email": "<EMAIL>"}), content_type="application/json")

        self.assertEqual(201, r1.status_code)
        self.assertEqual(400, r2.status_code)
        self.assertEqual(400, r3.status_code)
        self.assertEqual(400, r4.status_code)
        self.assertEqual(400, r5.status_code)
        self.assertEqual(400, r6.status_code)

class JwtAuthTestCase(TestCase):
    def setUp(self):
        User.objects.create_user(username="codert", password="<PASSWORD>", email="<EMAIL>", nickname="xx")

        self.encoder = json.JSONEncoder()

    def test_token_request(self):
        c1 = Client()
        c2 = Client()
        c3 = Client()
        c4 = Client()
        encoder = self.encoder

        # the user info is valid
        r1 = c1.post('/api/auth/', encoder.encode({ "username": "codert", "pwd": "<PASSWORD>" }), content_type="application/json")
        # missing `pwd` field
        r2 = c2.post('/api/auth/', encoder.encode({ "username": "codert", "xx": "xxxxxxxx" }), content_type="application/json")
        # the password is incorrect
        r3 = c3.post('/api/auth/', encoder.encode({ "username": "codert", "pwd": "<PASSWORD>" }), content_type="application/json")
        # the user isn't exsit
        r4 = c4.post('/api/auth/', encoder.encode({ "username": "donotexist", "pwd": "<PASSWORD>" }), content_type="application/json")

        self.assertEqual(201, r1.status_code)
        self.assertEqual(r2.json()['detail'], 'Incomplete authentication information')
        self.assertEqual(r3.json()['detail'], 'Incorrect authentication information')
        self.assertEqual(r3.json()['detail'], 'Incorrect authentication information')
{'fraction_non_alphanumeric': 0.17529215358931552, 'fraction_numerical': 0.01857262103505843, 'mean_word_length': 3.521698113207547, 'pattern_counts': {'":': 38, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '1511623', 'n_tokens_mistral': 1480, 'n_tokens_neox': 1437, 'n_words': 317}