Project

General

Profile

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

haketilo / test / unit / test_repo_query_cacher.py @ 7218849a

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

    
3
"""
4
Haketilo unit tests - caching responses from remote repositories
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 .utils import *
26

    
27
def content_script():
28
    script = load_script('content/repo_query_cacher.js')
29
    return f'{script}; {tab_id_asker}; start();'
30

    
31
def bypass_js():
32
    return load_script('background/CORS_bypass_server.js') + '; start();'
33

    
34
def fetch_through_cache(driver, tab_id, url):
35
    return driver.execute_script(
36
        '''
37
        return browser.tabs.sendMessage(arguments[0],
38
                                        ["repo_query", arguments[1]]);
39
        ''',
40
        tab_id, url)
41

    
42
@pytest.mark.ext_data({
43
    'content_script': content_script,
44
    'background_script': lambda: bypass_js() + ';' + tab_id_responder
45
})
46
@pytest.mark.usefixtures('webextension')
47
def test_repo_query_cacher_normal_use(driver, execute_in_page):
48
    """
49
    Test if HTTP requests made through our cacher return correct results.
50
    """
51
    tab_id = run_content_script_in_new_window(driver, 'https://gotmyowndoma.in')
52

    
53
    result = fetch_through_cache(driver, tab_id, 'https://counterdoma.in/')
54
    assert set(result.keys()) == {'ok', 'status', 'json'}
55
    counter_initial = result['json']['counter']
56
    assert type(counter_initial) is int
57

    
58
    for i in range(2):
59
        result = fetch_through_cache(driver, tab_id, 'https://counterdoma.in/')
60
        assert result['json']['counter'] == counter_initial
61

    
62
    tab_id = run_content_script_in_new_window(driver, 'https://gotmyowndoma.in')
63
    result = fetch_through_cache(driver, tab_id, 'https://counterdoma.in/')
64
    assert result['json']['counter'] == counter_initial + 1
65

    
66
    for i in range(2):
67
        result = fetch_through_cache(driver, tab_id, 'https://nxdoma.in/')
68
        assert set(result.keys()) == {'ok', 'status', 'error_json'}
69
        assert result['ok'] == False
70
        assert result['status'] == 404
71

    
72
    for i in range(2):
73
        result = fetch_through_cache(driver, tab_id, 'bad://url')
74
        assert set(result.keys()) == {'error'}
75

    
76
@pytest.mark.ext_data({
77
    'content_script': content_script,
78
    'background_script': tab_id_responder
79
})
80
@pytest.mark.usefixtures('webextension')
81
def test_repo_query_cacher_bgscript_error(driver):
82
    """
83
    Test if our cacher properly reports errors in communication with the
84
    background script.
85
    """
86
    tab_id = run_content_script_in_new_window(driver, 'https://gotmyowndoma.in')
87

    
88
    result = fetch_through_cache(driver, tab_id, 'https://counterdoma.in/')
89
    assert set(result.keys()) == {'error'}
(18-18/22)