<commit_before>from urlparse import urljoin
from clientlib import (
    make_example_shot,
    make_random_id,
    screenshots_session,
    example_images
)
import random

# Hack to make this predictable:
random.seed(0)


def test_invalid_image_url():
    with screenshots_session() as user:
        shot_id = make_random_id() + "/test.com"
        shot_data = urljoin(user.backend, "data/" + shot_id)

        shot_json = make_example_shot(user.deviceId)
        invalid_url = "https://example.com/?aaA=bbb=\"); background-color: red;"
        for clip_id in shot_json['clips']:
            shot_json['clips'][clip_id]['image']['url'] = invalid_url
            break

        resp = user.session.put(
            shot_data,
            json=shot_json,
        )
        print(resp.text)
        assert resp.status_code == 500  # assertion failure on clip image url


def test_invalid_data_image():
    with screenshots_session() as user:
        shot_id = make_random_id() + "/test.com"
        shot_data = urljoin(user.backend, "data/" + shot_id)
        shot_json = make_example_shot(user.deviceId)
        for entry in example_images:
            valid_data_image = entry['url']
            if "iVBORw0KGgo" in valid_data_image:
                invalid_data_image = valid_data_image.replace('iVBORw0KGgo', 'R0k')
                for clip_id in shot_json['clips']:
                    shot_json['clips'][clip_id]['image'] = invalid_data_image
                    break

        resp = user.session.put(
            shot_data,
            json=shot_json,
        )
        print(resp.text)
        assert resp.status_code == 500


def test_invalid_data_image_decoded():
    with screenshots_session() as user:
        shot_id = make_random_id() + "/test.com"
        shot_data = urljoin(user.backend, "data/" + shot_id)
        shot_json = make_example_shot(user.deviceId)
        for image in example_images:
            valid_data_image = image['url']
            if "iVBORw0KGgo" in valid_data_image:
                invalid_data_image = valid_data_image.replace('iVBORw0KGgo', 'someIM4gEgo')
                for clip_id in shot_json['clips']:
                    shot_json['clips'][clip_id]['image'] = invalid_data_image
                    break

        resp = user.session.put(
            shot_data,
            json=shot_json,
        )
        print(resp.text)
        assert resp.status_code == 500


def test_invalid_data_url():
    pass


if __name__ == "__main__":
    test_invalid_data_image()
    test_invalid_data_image_decoded()
    test_invalid_data_url()<commit_msg>Add test for invalid data url, reorganize the other tests<commit_after>from urlparse import urljoin
from clientlib import (
    make_example_shot,
    make_random_id,
    screenshots_session,
    example_images
)
import random

# Hack to make this predictable:
random.seed(0)


def test_invalid_data_image():
    with screenshots_session() as user:
        shot_id = make_random_id() + "/test.com"
        shot_data = urljoin(user.backend, "data/" + shot_id)
        shot_json = make_example_shot(user.deviceId)
        for image in example_images:
            valid_data_image = image['url']
            if "iVBORw0KGgo" in valid_data_image:
                invalid_data_image = valid_data_image.replace('iVBORw0KGgo', 'R0k')
                for clip_id in shot_json['clips']:
                    shot_json['clips'][clip_id]['image']['url'] = invalid_data_image
                    break

        resp = user.session.put(
            shot_data,
            json=shot_json,
        )
        print(resp.text)
        assert resp.status_code == 500


def test_invalid_data_image_decoded():
    with screenshots_session() as user:
        shot_id = make_random_id() + "/test.com"
        shot_data = urljoin(user.backend, "data/" + shot_id)
        shot_json = make_example_shot(user.deviceId)
        for image in example_images:
            valid_data_image = image['url']
            if "iVBORw0KGgo" in valid_data_image:
                invalid_data_image = valid_data_image.replace('iVBORw0KGgo', 'someIM4gEgo')
                for clip_id in shot_json['clips']:
                    shot_json['clips'][clip_id]['image'] = invalid_data_image
                    break

        resp = user.session.put(
            shot_data,
            json=shot_json,
        )
        print(resp.text)
        assert resp.status_code == 500


def test_invalid_data_url():
    with screenshots_session() as user:
        shot_id = make_random_id() + "/test.com"
        shot_data = urljoin(user.backend, "data/" + shot_id)
        shot_json = make_example_shot(user.deviceId)
        for image in example_images:
            valid_data_image = image['url']
            if "data:image/png;base64" in valid_data_image:
                invalid_data_url = valid_data_image.replace('data:image/png', 'data:image/foo')
                for clip_id in shot_json['clips']:
                    shot_json['clips'][clip_id]['image'] = invalid_data_url
                    break

        resp = user.session.put(
            shot_data,
            json=shot_json,
        )
        print(resp.text)
        assert resp.status_code == 500


if __name__ == "__main__":
    test_invalid_data_image()
    test_invalid_data_image_decoded()
    test_invalid_data_url()
{'fraction_non_alphanumeric': 0.09729526671675431, 'fraction_numerical': 0.006386175807663411, 'mean_word_length': 2.303349875930521, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '5433574', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1628, 'n_words': 316}