2017-02-13 22:42:37 +00:00
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
|
2017-04-21 21:37:00 +00:00
|
|
|
from mock import Mock
|
|
|
|
|
|
|
|
from buildtrigger.test.gitlabmock import get_gitlab_trigger, get_mock_gitlab
|
2017-02-13 22:42:37 +00:00
|
|
|
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):
|
2017-03-02 16:24:04 +00:00
|
|
|
assert gitlab_trigger.list_build_subdirs() == ['/Dockerfile']
|
2017-02-13 22:42:37 +00:00
|
|
|
|
|
|
|
|
2017-03-21 21:24:11 +00:00
|
|
|
@pytest.mark.parametrize('dockerfile_path, contents', [
|
2017-03-02 16:24:04 +00:00
|
|
|
('/Dockerfile', 'hello world'),
|
|
|
|
('somesubdir/Dockerfile', 'hi universe'),
|
2017-02-13 22:42:37 +00:00
|
|
|
('unknownpath', None),
|
|
|
|
])
|
2017-03-21 21:24:11 +00:00
|
|
|
def test_load_dockerfile_contents(dockerfile_path, contents):
|
|
|
|
trigger = get_gitlab_trigger(dockerfile_path)
|
2017-02-13 22:42:37 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2017-05-02 17:11:57 +00:00
|
|
|
def test_null_permissions(gitlab_trigger):
|
|
|
|
gitlab_trigger._get_authorized_client = get_mock_gitlab(with_nulls=True)
|
|
|
|
sources = gitlab_trigger.list_build_sources_for_namespace('someorg')
|
|
|
|
source = sources[0]
|
|
|
|
assert source['has_admin_permissions']
|
|
|
|
|
|
|
|
|
2017-04-21 21:37:00 +00:00
|
|
|
def test_null_avatar(gitlab_trigger):
|
2017-05-02 17:11:57 +00:00
|
|
|
gitlab_trigger._get_authorized_client = get_mock_gitlab(with_nulls=True)
|
2017-04-21 21:37:00 +00:00
|
|
|
namespace_data = gitlab_trigger.list_build_source_namespaces()
|
|
|
|
expected = {
|
|
|
|
'avatar_url': None,
|
|
|
|
'personal': False,
|
|
|
|
'title': 'someorg',
|
|
|
|
'url': 'https://bitbucket.org/someorg',
|
|
|
|
'score': 1,
|
|
|
|
'id': 'someorg',
|
|
|
|
}
|
|
|
|
|
|
|
|
assert namespace_data == [expected]
|
|
|
|
|
|
|
|
|
2017-02-13 22:42:37 +00:00
|
|
|
@pytest.mark.parametrize('payload, expected_error, expected_message', [
|
2017-03-20 17:22:59 +00:00
|
|
|
('{}', InvalidPayloadException, ''),
|
2017-02-13 22:42:37 +00:00
|
|
|
|
|
|
|
# Valid payload:
|
|
|
|
('''{
|
2017-02-28 22:13:00 +00:00
|
|
|
"object_kind": "push",
|
2017-02-13 22:42:37 +00:00
|
|
|
"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:
|
|
|
|
('''{
|
2017-02-28 22:13:00 +00:00
|
|
|
"object_kind": "push",
|
2017-02-13 22:42:37 +00:00
|
|
|
"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)
|
|
|
|
|
|
|
|
|