Project

General

Profile

Download (2.11 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / test / haketilo_test / unit / test_haketilo_apis.py @ fba67f09

1
# SPDX-License-Identifier: CC0-1.0
2

    
3
"""
4
Haketilo unit tests - exposing some special functionalities to injected scripts
5
"""
6

    
7
# This file is part of Haketilo
8
#
9
# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
10
#
11
# This program is free software: you can redistribute it and/or modify
12
# it under the terms of the CC0 1.0 Universal License as published by
13
# the Creative Commons Corporation.
14
#
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
# CC0 1.0 Universal License for more details.
19

    
20
import pytest
21
import json
22
from selenium.webdriver.support.ui import WebDriverWait
23

    
24
from ..script_loader import load_script
25
from ..world_wide_library import some_data
26

    
27
def content_script():
28
    return load_script('content/haketilo_apis.js') + ';\nstart();'
29

    
30
def background_script():
31
    return load_script('background/CORS_bypass_server.js') + ';\nstart();'
32

    
33
@pytest.mark.ext_data({
34
    'content_script': content_script,
35
    'background_script': background_script
36
})
37
@pytest.mark.usefixtures('webextension')
38
def test_haketilo_apis_CORS_bypass(driver):
39
    """
40
    Verify injected scripts will be able to bypass CORS with the help of
41
    Haketilo API.
42
    """
43
    driver.get('https://gotmyowndoma.in/')
44
    driver.execute_script(
45
        '''
46
        const fetch_arg = {
47
            url: "https://anotherdoma.in/resource/blocked/by/CORS.json",
48
            init: {}
49
        };
50

    
51
        const detail = {
52
            data: JSON.stringify(fetch_arg),
53
            id:   "abcdef"
54
        };
55

    
56
        window.addEventListener("haketilo_CORS_bypass-abcdef",
57
                                e => window.__response = e.detail);
58
        window.dispatchEvent(new CustomEvent("haketilo_CORS_bypass", {detail}));
59
        ''')
60

    
61
    get_response = lambda d: d.execute_script("return window.__response;")
62
    response = WebDriverWait(driver, 10).until(get_response)
63
    response = json.loads(response)
64

    
65
    assert response['body'] == some_data.encode().hex()
66
    assert response['status'] == 200
67
    assert type(response['headers']) is list
(8-8/26)