<commit_before>from tests.BaseTestWithDB import BaseTestWithDB
from tests.topics.TopicsTestDataGenerator import TopicsTestDataGenerator

# TODO: No idea how to test this one... more of an integration test really
class JobeProxyViewTest(BaseTestWithDB):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.language = "en"
        self.test_data = TopicsTestDataGenerator()

    # TODO Finish tests...<commit_msg>Add jobe proxy test for plugging it in<commit_after>import json
from unittest import mock

from django.urls import reverse
from tests.BaseTestWithDB import BaseTestWithDB


def mocked_requests_post(*args, **kwargs):
    class MockResponse:
        def __init__(self, text, status_code):
            self.text = text
            self.status_code = status_code

    # Create mock JOBE server response when the api key is set to 'mockapikey'
    if args[0] == 'http://mockjobeserver/jobe/index.php/restapi/runs/' \
       and 'X-API-KEY' in kwargs['headers'] \
       and kwargs['headers']['X-API-KEY'] == 'mockapikey':
        return MockResponse('Hello World (with API key)', 200)

    # Create a mock JOBE server response when no api key is set
    elif args[0] == 'http://mockjobeserver/jobe/index.php/restapi/runs/':
        return MockResponse('Hello World', 200)


class JobeProxyViewTest(BaseTestWithDB):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.language = "en"

    def get_test_program_data(self, source_code):
        return {
            "run_spec": {
                "language_id": "python3",
                "sourcefilename": "test_program",
                "sourcecode": source_code,
                "input": "input"
            }
        }

    @mock.patch('requests.post', side_effect=mocked_requests_post)
    def test_valid_jobe_proxy_call(self, mock_post):
        test_program_data = self.get_test_program_data("print('Hello World')")

        url = reverse("plugging_it_in:jobe_proxy")

        # Set the settings variable to call the mock JOBE server
        with self.settings(JOBE_SERVER_URL='http://mockjobeserver'):
            response = self.client.post(url, json.dumps(test_program_data), content_type='application/json')

        # Assert that the function returns the correct response from the mock JOBE server.
        self.assertEqual(response.content.decode(), 'Hello World')

    @mock.patch('requests.post', side_effect=mocked_requests_post)
    def test_valid_jobe_proxy_call_with_api_key(self, mock_post):
        test_program_data = self.get_test_program_data("print('Hello World') (with API key)")

        url = reverse("plugging_it_in:jobe_proxy")

        # Set the settings variable to call the mock JOBE server
        with self.settings(JOBE_SERVER_URL='http://mockjobeserver'), self.settings(JOBE_API_KEY='mockapikey'):
            response = self.client.post(url, json.dumps(test_program_data), content_type='application/json')

        # Assert that the function returns the correct response from the mock JOBE server.
        self.assertEqual(response.content.decode(), 'Hello World (with API key)')
{'fraction_non_alphanumeric': 0.1063627730294397, 'fraction_numerical': 0.002849002849002849, 'mean_word_length': 3.340659340659341, 'pattern_counts': {'":': 5, '<': 3, '<?xml version=': 0, '>': 3, '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': '1888782', 'n_tokens_mistral': 940, 'n_tokens_neox': 912, 'n_words': 233}