<commit_before>import json
import base64
import datetime

from django.test import TestCase
from django.contrib.auth.models import User
from django_dynamic_fixture import get
from rest_framework import status
from rest_framework.test import APIClient

from readthedocs.builds.models import Build


super_auth = base64.b64encode('super:test')
eric_auth = base64.b64encode('eric:test')


class APIBuildTests(TestCase):
    fixtures = ['eric.json', 'test_data.json']

    def test_make_build(self):
        """
        Test that a superuser can use the API
        """
        client = APIClient()
        client.login(username='super', password='<PASSWORD>')
        resp = client.post(
            '/api/v2/build/',
            {
                'project': 1,
                'version': 1,
                'success': True,
                'output': 'Test Output',
                'error': 'Test Error',
                'state': 'cloning',
            },
            format='json')
        self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
        build = resp.data
        self.assertEqual(build['id'], 1)
        self.assertEqual(build['state_display'], 'Cloning')

        resp = client.get('/api/v2/build/1/')
        self.assertEqual(resp.status_code, 200)
        build = resp.data
        self.assertEqual(build['output'], 'Test Output')
        self.assertEqual(build['state_display'], 'Cloning')

    def test_make_build_without_permission(self):
        """Ensure anonymous/non-staff users cannot write the build endpoint"""
        client = APIClient()

        def _try_post():
            resp = client.post(
                '/api/v2/build/',
                {
                    'project': 1,
                    'version': 1,
                    'success': True,
                    'output': 'Test Output',
                    'error': 'Test Error',
                },
                format='json')
            self.assertEqual(resp.status_code, 403)

        _try_post()

        api_user = get(User, staff=False, password='<PASSWORD>')
        assert api_user.is_staff is False
        client.force_authenticate(user=api_user)
        _try_post()

    def test_update_build_without_permission(self):
        """Ensure anonymous/non-staff users cannot update build endpoints"""
        client = APIClient()
        api_user = get(User, staff=False, password='<PASSWORD>')
        client.force_authenticate(user=api_user)
        build = get(Build, project_id=1, version_id=1, state='cloning')
        resp = client.put(
            '/api/v2/build/{0}/'.format(build.pk),
            {
                'project': 1,
                'version': 1,
                'state': 'finished'
            },
            format='json')
        self.assertEqual(resp.status_code, 403)

    def test_make_build_protected_fields(self):
        """Ensure build api view delegates correct serializer

        Super users should be able to read/write the `builder` property, but we
        don't expose this to end users via the API
        """
        build = get(Build, project_id=1, version_id=1, builder='foo')
        client = APIClient()

        api_user = get(User, staff=False, password='<PASSWORD>')
        client.force_authenticate(user=api_user)
        resp = client.get('/api/v2/build/{0}/'.format(build.pk), format='json')
        self.assertEqual(resp.status_code, 403)

        client.force_authenticate(user=User.objects.get(username='super'))
        resp = client.get('/api/v2/build/{0}/'.format(build.pk), format='json')
        self.assertEqual(resp.status_code, 200)
        self.assertIn('builder', resp.data)

    def test_make_build_commands(self):
        """Create build and build commands"""
        client = APIClient()
        client.login(username='super', password='<PASSWORD>')
        resp = client.post(
            '/api/v2/build/',
            {
                'project': 1,
                'version': 1,
                'success': True,
            },
            format='json')
        self.assertEqual(resp.status_code, status.HTTP_201_CREATED)<commit_msg>Fix api test not setting is_staff properly<commit_after>import json
import base64
import datetime

from django.test import TestCase
from django.contrib.auth.models import User
from django_dynamic_fixture import get
from rest_framework import status
from rest_framework.test import APIClient

from readthedocs.builds.models import Build


super_auth = base64.b64encode('super:test')
eric_auth = base64.b64encode('eric:test')


class APIBuildTests(TestCase):
    fixtures = ['eric.json', 'test_data.json']

    def test_make_build(self):
        """
        Test that a superuser can use the API
        """
        client = APIClient()
        client.login(username='super', password='<PASSWORD>')
        resp = client.post(
            '/api/v2/build/',
            {
                'project': 1,
                'version': 1,
                'success': True,
                'output': 'Test Output',
                'error': 'Test Error',
                'state': 'cloning',
            },
            format='json')
        self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
        build = resp.data
        self.assertEqual(build['id'], 1)
        self.assertEqual(build['state_display'], 'Cloning')

        resp = client.get('/api/v2/build/1/')
        self.assertEqual(resp.status_code, 200)
        build = resp.data
        self.assertEqual(build['output'], 'Test Output')
        self.assertEqual(build['state_display'], 'Cloning')

    def test_make_build_without_permission(self):
        """Ensure anonymous/non-staff users cannot write the build endpoint"""
        client = APIClient()

        def _try_post():
            resp = client.post(
                '/api/v2/build/',
                {
                    'project': 1,
                    'version': 1,
                    'success': True,
                    'output': 'Test Output',
                    'error': 'Test Error',
                },
                format='json')
            self.assertEqual(resp.status_code, 403)

        _try_post()

        api_user = get(User, is_staff=False, password='<PASSWORD>')
        assert api_user.is_staff is False
        client.force_authenticate(user=api_user)
        _try_post()

    def test_update_build_without_permission(self):
        """Ensure anonymous/non-staff users cannot update build endpoints"""
        client = APIClient()
        api_user = get(User, is_staff=False, password='<PASSWORD>')
        client.force_authenticate(user=api_user)
        build = get(Build, project_id=1, version_id=1, state='cloning')
        resp = client.put(
            '/api/v2/build/{0}/'.format(build.pk),
            {
                'project': 1,
                'version': 1,
                'state': 'finished'
            },
            format='json')
        self.assertEqual(resp.status_code, 403)

    def test_make_build_protected_fields(self):
        """Ensure build api view delegates correct serializer

        Super users should be able to read/write the `builder` property, but we
        don't expose this to end users via the API
        """
        build = get(Build, project_id=1, version_id=1, builder='foo')
        client = APIClient()

        api_user = get(User, is_staff=False, password='<PASSWORD>')
        client.force_authenticate(user=api_user)
        resp = client.get('/api/v2/build/{0}/'.format(build.pk), format='json')
        self.assertEqual(resp.status_code, 403)

        client.force_authenticate(user=User.objects.get(username='super'))
        resp = client.get('/api/v2/build/{0}/'.format(build.pk), format='json')
        self.assertEqual(resp.status_code, 200)
        self.assertIn('builder', resp.data)

    def test_make_build_commands(self):
        """Create build and build commands"""
        client = APIClient()
        client.login(username='super', password='<PASSWORD>')
        resp = client.post(
            '/api/v2/build/',
            {
                'project': 1,
                'version': 1,
                'success': True,
            },
            format='json')
        self.assertEqual(resp.status_code, status.HTTP_201_CREATED)
{'fraction_non_alphanumeric': 0.12339393939393939, 'fraction_numerical': 0.013333333333333334, 'mean_word_length': 2.0983852797596696, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '4900775', 'n_tokens_mistral': 2423, 'n_tokens_neox': 2313, 'n_words': 497}