import json import pytest from buildtrigger.test.gitlabmock import get_gitlab_trigger from buildtrigger.triggerutil import (SkipRequestException, ValidationRequestException, InvalidPayloadException) from endpoints.building import PreparedBuild from util.morecollections import AttrDict @pytest.fixture def gitlab_trigger(): return get_gitlab_trigger() def test_list_build_subdirs(gitlab_trigger): assert gitlab_trigger.list_build_subdirs() == [''] @pytest.mark.parametrize('subdir, contents', [ ('', 'hello world'), ('somesubdir', 'hi universe'), ('unknownpath', None), ]) def test_load_dockerfile_contents(subdir, contents): trigger = get_gitlab_trigger(subdir) assert trigger.load_dockerfile_contents() == contents @pytest.mark.parametrize('email, expected_response', [ ('unknown@email.com', None), ('knownuser', {'username': 'knownuser', 'html_url': 'https://bitbucket.org/knownuser', 'avatar_url': 'avatarurl'}), ]) def test_lookup_user(email, expected_response, gitlab_trigger): assert gitlab_trigger.lookup_user(email) == expected_response @pytest.mark.parametrize('payload, expected_error, expected_message', [ ('{}', SkipRequestException, ''), # Valid payload: ('''{ "object_kind": "push", "ref": "refs/heads/master", "checkout_sha": "aaaaaaa", "repository": { "git_ssh_url": "foobar" }, "commits": [ { "id": "aaaaaaa", "url": "someurl", "message": "hello there!", "timestamp": "now" } ] }''', None, None), # Skip message: ('''{ "object_kind": "push", "ref": "refs/heads/master", "checkout_sha": "aaaaaaa", "repository": { "git_ssh_url": "foobar" }, "commits": [ { "id": "aaaaaaa", "url": "someurl", "message": "[skip build] hello there!", "timestamp": "now" } ] }''', SkipRequestException, ''), ]) def test_handle_trigger_request(gitlab_trigger, payload, expected_error, expected_message): def get_payload(): return json.loads(payload) request = AttrDict(dict(get_json=get_payload)) if expected_error is not None: with pytest.raises(expected_error) as ipe: gitlab_trigger.handle_trigger_request(request) assert ipe.value.message == expected_message else: assert isinstance(gitlab_trigger.handle_trigger_request(request), PreparedBuild)