-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
"""Integration tests configuration file.""" | ||
import pytest | ||
|
||
# pylint: disable=unused-import | ||
from wppt.app import app as flask_app | ||
|
||
@pytest.fixture(scope="module") | ||
def test_client(): | ||
""" | ||
| Creates a test client for the app. | ||
""" | ||
with flask_app.test_client() as testing_client: | ||
with flask_app.app_context(): | ||
yield testing_client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
test: | ||
enabled: true | ||
target_webhook: https://example.com/rest/api/ | ||
translations: | ||
data: | ||
name: '[{data[project][name]}][{data[object_kind]}] {data[object_attributes][title]}' | ||
description: 'Description: {data[object_attributes][description]}\nURL:{data[object_attributes][url]}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from unittest.mock import patch | ||
from pytest import raises | ||
import requests | ||
|
||
class TestApp: | ||
|
||
@patch('wppt.app.parse_definitions') | ||
@patch('wppt.app.traverse_format_dict') | ||
@patch('wppt.app.requests.post') | ||
def test_dinamic_transformer_enabled_transformer(self, mock_post, mock_traverse_format_dict, mock_parse_definitions, test_client): | ||
mock_parse_definitions.return_value = { | ||
'transformer1': { | ||
'enabled': True, | ||
'translations': {'key1': 'value1'}, | ||
'target_webhook': 'https://example.com/webhook' | ||
} | ||
} | ||
mock_traverse_format_dict.return_value = None | ||
mock_post.return_value.status_code = 200 | ||
|
||
response = test_client.post('/transformer1/', json={'key1': 'value1'}) | ||
|
||
assert response.status_code == 200 | ||
assert response.json == {'status_code': 200, 'message': 'Transformers executed.'} | ||
mock_traverse_format_dict.assert_called_once_with({'key1': 'value1'}, {'key1': 'value1'}) | ||
|
||
@patch('wppt.app.parse_definitions') | ||
def test_dinamic_transformer_disabled_transformer(self, mock_parse_definitions, test_client): | ||
mock_parse_definitions.return_value = { | ||
'transformer1': { | ||
'enabled': False, | ||
'translations': {'key1': 'value1'}, | ||
'target_webhook': 'https://example.com/webhook' | ||
} | ||
} | ||
response = test_client.post('/transformer1/', json={'key1': 'value1'}) | ||
|
||
assert response.json[0] == 'Transformer transformer1 is disabled' | ||
assert response.json[1] == 400 | ||
|
||
@patch('wppt.app.parse_definitions') | ||
@patch('wppt.app.traverse_format_dict', side_effect=KeyError('Invalid key')) | ||
def test_dinamic_transformer_invalid_key(self, mock_traverse_format_dict, mock_parse_definitions, test_client): | ||
mock_parse_definitions.return_value = { | ||
'transformer1': { | ||
'enabled': True, | ||
'translations': {'key1': 'value1'}, | ||
'target_webhook': 'https://example.com/webhook' | ||
} | ||
} | ||
|
||
response = test_client.post('/transformer1/', json={'key1': 'value1'}) | ||
|
||
assert response.json[0] == "'Invalid key'" | ||
assert response.json[1] == 400 | ||
mock_traverse_format_dict.assert_called_once_with({'key1': 'value1'}, {'key1': 'value1'}) | ||
|
||
@patch('wppt.app.parse_definitions') | ||
@patch('wppt.app.traverse_format_dict') | ||
@patch('wppt.app.requests.post', side_effect=requests.exceptions.RequestException('Failed to send request')) | ||
def test_dinamic_transformer_failed_request(self, mock_post, mock_traverse_format_dict, mock_parse_definitions, test_client): | ||
mock_parse_definitions.return_value = { | ||
'transformer1': { | ||
'enabled': True, | ||
'translations': {'key1': 'value1'}, | ||
'target_webhook': 'https://example.com/webhook' | ||
} | ||
} | ||
mock_traverse_format_dict.return_value = None | ||
mock_post.side_effect = requests.exceptions.RequestException('Failed to send request') | ||
|
||
response = test_client.post('/transformer1/', json={'key1': 'value1'}) | ||
|
||
assert response.json['message'] == 'Failed to send the transformed webhook for transformer1.' | ||
assert response.json['status_code'] == 400 | ||
mock_traverse_format_dict.assert_called_once_with({'key1': 'value1'}, {'key1': 'value1'}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from unittest.mock import patch | ||
from wppt.utils import parse_definitions, walk_dir, read_yaml, traverse_format_dict | ||
|
||
class TestUtils: | ||
|
||
@patch('wppt.utils.walk_dir') | ||
@patch('wppt.utils.read_yaml') | ||
def test_parse_definitions(self, mock_read_yaml, mock_walk_dir): | ||
mock_walk_dir.return_value = ['/path/to/file1.yaml', '/path/to/file2.yaml'] | ||
mock_read_yaml.side_effect = [{'key1': 'value1'}, {'key2': 'value2'}] | ||
|
||
directory = '/path/to/directory' | ||
expected_definitions = {'key1': 'value1', 'key2': 'value2'} | ||
assert parse_definitions(directory) == expected_definitions | ||
|
||
def test_walk_dir(self): | ||
directory = 'tests/fixtures/transformers' | ||
file_extension = '.yaml' | ||
expected_file_list = ['tests/fixtures/transformers/test.yaml'] | ||
|
||
assert walk_dir(directory, file_extension) == expected_file_list | ||
|
||
def test_read_yaml(self): | ||
file_path = '../wppt/transformers/gitlab2jira.yaml' | ||
expected_yaml_definitions = {'key1': 'value1', 'key2': 'value2'} | ||
|
||
# Mocking the yaml.load function | ||
with patch('yaml.load') as mock_yaml_load: | ||
mock_yaml_load.return_value = {'key1': 'value1', 'key2': 'value2'} | ||
|
||
file_path = '../wppt/transformers/gitlab2jira.yaml' | ||
expected_yaml_definitions = {'key1': 'value1', 'key2': 'value2'} | ||
|
||
with patch('builtins.open', create=True) as mock_open: | ||
mock_open.return_value.__enter__.return_value.read.return_value = 'key1: value1\nkey2: value2\n' | ||
yaml_definitions = read_yaml(file_path) | ||
assert yaml_definitions == expected_yaml_definitions | ||
|
||
def test_traverse_format_dict(self): | ||
dictionary = {'key1': 'Hello {data}', 'key2': 'Welcome {data}'} | ||
data = 'World' | ||
expected_dictionary = {'key1': 'Hello World', 'key2': 'Welcome World'} | ||
|
||
traverse_format_dict(dictionary, data) | ||
assert dictionary == expected_dictionary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters