Merge #64 Add unit tests for API routes

This commit is contained in:
Matt Prahl
2016-09-12 16:38:55 +00:00
4 changed files with 450 additions and 0 deletions

View File

@@ -57,6 +57,11 @@ class DevConfiguration(BaseConfiguration):
#FAS_PASSWORD = os.environ('FAS_PASSWORD') # you could store it here
#FAS_PASSWORD = commands.getoutput('pass your_fas_password').strip()
class TestConfiguration(BaseConfiguration):
LOG_BACKEND = 'console'
SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
DEBUG = True
class ProdConfiguration(BaseConfiguration):
FAS_USERNAME = 'TODO'

View File

@@ -0,0 +1,172 @@
# Copyright (c) 2016 Red Hat, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Written by Matt Prahl <mprahl@redhat.com
from datetime import datetime, timedelta
from rida import app, db
from rida.models import ModuleBuild, ComponentBuild
app.config.from_object('config.TestConfiguration')
def init_data():
db.session.remove()
db.drop_all()
db.create_all()
for index in range(10):
build_one = ModuleBuild()
build_one.name = 'nginx'
build_one.version = '1'
build_one.release = 2
build_one.state = 3
build_one.modulemd = '' # Skipping since no tests rely on it
build_one.koji_tag = 'module-nginx-1.2'
build_one.scmurl = ('git://pkgs.domain.local/modules/nginx?'
'#ba95886c7a443b36a9ce31abda1f9bef22f2f8c9')
build_one.batch = 2
# https://www.youtube.com/watch?v=iQGwrK_yDEg
build_one.owner = 'Moe Szyslak'
build_one.time_submitted = \
datetime(2016, 9, 3, 11, 23, 20) + timedelta(minutes=(index * 10))
build_one.time_modified = \
datetime(2016, 9, 3, 11, 25, 32) + timedelta(minutes=(index * 10))
build_one.time_completed = \
datetime(2016, 9, 3, 11, 25, 32) + timedelta(minutes=(index * 10))
component_one_build_one = ComponentBuild()
component_one_build_one.package = 'nginx'
component_one_build_one.scmurl = \
('git://pkgs.domain.local/rpms/nginx?'
'#ga95886c8a443b36a9ce31abda1f9bed22f2f8c3')
component_one_build_one.format = 'rpms'
component_one_build_one.task_id = 12312345 + index
component_one_build_one.state = 1
component_one_build_one.nvr = 'nginx-1.10.1-2.module_nginx_1_2'
component_one_build_one.batch = 1
component_one_build_one.module_id = 1 + index * 3
component_two_build_one = ComponentBuild()
component_two_build_one.package = 'module-build-macros'
component_two_build_one.scmurl = \
('/tmp/rida-build-macrosWZUPeK/SRPMS/'
'module-build-macros-0.1-1.module_nginx_1_2.src.rpm')
component_two_build_one.format = 'rpms'
component_two_build_one.task_id = 12312321 + index
component_two_build_one.state = 1
component_two_build_one.nvr = \
'module-build-macros-01-1.module_nginx_1_2'
component_two_build_one.batch = 2
component_two_build_one.module_id = 1 + index * 3
build_two = ModuleBuild()
build_two.name = 'postgressql'
build_two.version = '1'
build_two.release = 2
build_two.state = 3
build_two.modulemd = '' # Skipping since no tests rely on it
build_two.koji_tag = 'module-postgressql-1.2'
build_two.scmurl = ('git://pkgs.domain.local/modules/postgressql?'
'#aa95886c7a443b36a9ce31abda1f9bef22f2f8c9')
build_two.batch = 2
build_two.owner = 'some_user'
build_two.time_submitted = \
datetime(2016, 9, 3, 12, 25, 33) + timedelta(minutes=(index * 10))
build_two.time_modified = \
datetime(2016, 9, 3, 12, 27, 19) + timedelta(minutes=(index * 10))
build_two.time_completed = \
datetime(2016, 9, 3, 11, 27, 19) + timedelta(minutes=(index * 10))
component_one_build_two = ComponentBuild()
component_one_build_two.package = 'postgresql'
component_one_build_two.scmurl = \
('git://pkgs.domain.local/rpms/postgresql?'
'#dc95586c4a443b26a9ce38abda1f9bed22f2f8c3')
component_one_build_two.format = 'rpms'
component_one_build_two.task_id = 2433433 + index
component_one_build_two.state = 1
component_one_build_two.nvr = 'postgresql-9.5.3-4.module_postgresql_1_2'
component_one_build_two.batch = 2
component_one_build_two.module_id = 2 + index * 3
component_two_build_two = ComponentBuild()
component_two_build_two.package = 'module-build-macros'
component_two_build_two.scmurl = \
('/tmp/rida-build-macrosWZUPeK/SRPMS/'
'module-build-macros-0.1-1.module_postgresql_1_2.src.rpm')
component_two_build_two.format = 'rpms'
component_two_build_two.task_id = 47383993 + index
component_two_build_two.state = 1
component_two_build_two.nvr = \
'module-build-macros-01-1.module_postgresql_1_2'
component_two_build_two.batch = 1
component_two_build_two.module_id = 2 + index * 3
build_three = ModuleBuild()
build_three.name = 'testmodule'
build_three.version = '4.3.43'
build_three.release = 6
build_three.state = 1
build_three.modulemd = '' # Skipping because no tests rely on it
build_three.koji_tag = None
build_three.scmurl = ('git://pkgs.domain.local/modules/testmodule?'
'#ca95886c7a443b36a9ce31abda1f9bef22f2f8c9')
build_three.batch = 0
build_three.owner = 'some_other_user'
build_three.time_submitted = \
datetime(2016, 9, 3, 12, 28, 33) + timedelta(minutes=(index * 10))
build_three.time_modified = \
datetime(2016, 9, 3, 12, 28, 40) + timedelta(minutes=(index * 10))
build_three.time_completed = None
component_one_build_three = ComponentBuild()
component_one_build_three.package = 'rubygem-rails'
component_one_build_three.scmurl = \
('git://pkgs.domain.local/rpms/rubygem-rails?'
'#dd55886c4a443b26a9ce38abda1f9bed22f2f8c3')
component_one_build_three.format = 'rpms'
component_one_build_three.task_id = 2433433 + index
component_one_build_three.state = 3
component_one_build_three.nvr = 'postgresql-9.5.3-4.module_postgresql_1_2'
component_one_build_three.batch = 2
component_one_build_three.module_id = 3 + index * 3
component_two_build_three = ComponentBuild()
component_two_build_three.package = 'module-build-macros'
component_two_build_three.scmurl = \
('/tmp/rida-build-macrosWZUPeK/SRPMS/'
'module-build-macros-0.1-1.module_testmodule_1_2.src.rpm')
component_two_build_three.format = 'rpms'
component_two_build_three.task_id = 47383993 + index
component_two_build_three.state = 1
component_two_build_three.nvr = \
'module-build-macros-01-1.module_postgresql_1_2'
component_two_build_three.batch = 1
component_two_build_three.module_id = 3 + index * 3
db.session.add(build_one)
db.session.add(component_one_build_one)
db.session.add(component_two_build_one)
db.session.add(component_one_build_two)
db.session.add(component_two_build_two)
db.session.add(component_one_build_three)
db.session.add(component_two_build_three)
db.session.add(build_two)
db.session.add(build_three)
db.session.commit()

View File

@@ -0,0 +1,32 @@
document: modulemd
version: 0
data:
name: fakemodule
version: 4.3.44
release: 5
summary: A fake module containing the bash shell
description: >
A fake module used for testing
license:
module:
- MIT
content: []
xmd: ~
dependencies:
buildrequires:
base_runtime: 1.0-0
references:
community: https://fedoraproject.org/wiki/Modularity
tracker: https://taiga.fedorainfracloud.org/project/modularity
profiles:
default:
rpms:
- bash
components:
rpms:
api:
- bash
packages:
bash:
rationale: It's here to test the whole thing!
commit: 70fa7516b83768595a4f3280ae890a7ac957e0c7

View File

@@ -0,0 +1,241 @@
# Copyright (c) 2016 Red Hat, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Written by Matt Prahl <mprahl@redhat.com
import unittest
import json
from mock import patch
from shutil import copyfile
from os import path, mkdir
from datetime import datetime
from tests import app, init_data
class TestViews(unittest.TestCase):
def setUp(self):
self.client = app.test_client()
init_data()
def test_query_build(self):
rv = self.client.get('/rida/module-builds/1')
data = json.loads(rv.data)
self.assertEquals(data['id'], 1)
self.assertEquals(data['name'], 'nginx')
self.assertEquals(data['owner'], 'Moe Szyslak')
self.assertEquals(data['state'], 3)
self.assertEquals(data['tasks'], {
'rpms/module-build-macros': '12312321/1',
'rpms/nginx': '12312345/1'}
)
self.assertEquals(data['time_completed'], '2016-09-03T11:25:32Z')
self.assertEquals(data['time_modified'], '2016-09-03T11:25:32Z')
self.assertEquals(data['time_submitted'], '2016-09-03T11:23:20Z')
def test_pagination_metadata(self):
rv = self.client.get('/rida/module-builds/?per_page=8&page=2')
meta_data = json.loads(rv.data)['meta']
self.assertTrue(
'rida/module-builds/?per_page=8&page=1' in meta_data['prev'])
self.assertTrue(
'rida/module-builds/?per_page=8&page=3' in meta_data['next'])
self.assertTrue(
'rida/module-builds/?per_page=8&page=4' in meta_data['last'])
self.assertTrue(
'rida/module-builds/?per_page=8&page=1' in meta_data['first'])
self.assertEquals(meta_data['total'], 30)
self.assertEquals(meta_data['per_page'], 8)
self.assertEquals(meta_data['pages'], 4)
self.assertEquals(meta_data['page'], 2)
def test_query_builds(self):
rv = self.client.get('/rida/module-builds/?per_page=2')
items = json.loads(rv.data)['items']
self.assertEquals(items,
[{u'state': 3, u'id': 1}, {u'state': 3, u'id': 2}])
def test_query_builds_verbose(self):
rv = self.client.get('/rida/module-builds/?per_page=2&verbose=True')
item = json.loads(rv.data)['items'][1]
self.assertEquals(item['id'], 2)
self.assertEquals(item['name'], 'postgressql')
self.assertEquals(item['owner'], 'some_user')
self.assertEquals(item['state'], 3)
self.assertEquals(item['tasks'], {
'rpms/module-build-macros': '47383993/1',
'rpms/postgresql': '2433433/1'
}
)
self.assertEquals(item['time_completed'], '2016-09-03T11:27:19Z')
self.assertEquals(item['time_modified'], '2016-09-03T12:27:19Z')
self.assertEquals(item['time_submitted'], '2016-09-03T12:25:33Z')
def test_query_builds_filter_name(self):
rv = self.client.get('/rida/module-builds/?name=nginx')
data = json.loads(rv.data)
self.assertEquals(data['meta']['total'], 10)
def test_query_builds_filter_completed_before(self):
rv = self.client.get(
'/rida/module-builds/?completed_before=2016-09-03T11:30:00Z')
data = json.loads(rv.data)
self.assertEquals(data['meta']['total'], 2)
def test_query_builds_filter_completed_after(self):
rv = self.client.get(
'/rida/module-builds/?completed_after=2016-09-03T12:25:00Z')
data = json.loads(rv.data)
self.assertEquals(data['meta']['total'], 8)
def test_query_builds_filter_submitted_before(self):
rv = self.client.get(
'/rida/module-builds/?submitted_before=2016-09-03T12:25:00Z')
data = json.loads(rv.data)
self.assertEquals(data['meta']['total'], 7)
def test_query_builds_filter_submitted_after(self):
rv = self.client.get(
'/rida/module-builds/?submitted_after=2016-09-03T12:25:00Z')
data = json.loads(rv.data)
self.assertEquals(data['meta']['total'], 23)
def test_query_builds_filter_modified_before(self):
rv = self.client.get(
'/rida/module-builds/?modified_before=2016-09-03T12:25:00Z')
data = json.loads(rv.data)
self.assertEquals(data['meta']['total'], 6)
def test_query_builds_filter_modified_after(self):
rv = self.client.get(
'/rida/module-builds/?modified_after=2016-09-03T12:25:00Z')
data = json.loads(rv.data)
self.assertEquals(data['meta']['total'], 24)
def test_query_builds_filter_owner(self):
rv = self.client.get(
'/rida/module-builds/?owner=Moe%20Szyslak')
data = json.loads(rv.data)
self.assertEquals(data['meta']['total'], 10)
def test_query_builds_filter_state(self):
rv = self.client.get(
'/rida/module-builds/?state=3')
data = json.loads(rv.data)
self.assertEquals(data['meta']['total'], 20)
def test_query_builds_two_filters(self):
rv = self.client.get('/rida/module-builds/?owner=Moe%20Szyslak'
'&modified_after=2016-09-03T12:25:00Z')
data = json.loads(rv.data)
self.assertEquals(data['meta']['total'], 4)
def test_query_builds_filter_invalid_date(self):
rv = self.client.get(
'/rida/module-builds/?modified_after=2016-09-03T12:25:00-05:00')
data = json.loads(rv.data)
self.assertEquals(data['error'], 'Bad Request')
self.assertEquals(data['message'], 'An invalid Zulu ISO 8601 timestamp'
' was provided for the \"modified_after\" parameter')
self.assertEquals(data['status'], 400)
@patch('rida.auth.is_packager', return_value='Homer J. Simpson')
@patch('rida.scm.SCM')
def test_submit_build(self, mocked_scm, mocked_is_packager):
def mocked_scm_checkout(temp_dir):
scm_dir = path.join(temp_dir, 'fakemodule')
mkdir(scm_dir)
base_dir = path.abspath(path.dirname(__file__))
copyfile(path.join(base_dir, 'fakemodule.yaml'),
path.join(scm_dir, 'fakemodule.yaml'))
return scm_dir
mocked_scm.return_value.checkout = mocked_scm_checkout
mocked_scm.return_value.name = 'fakemodule'
rv = self.client.post('/rida/module-builds/', data=json.dumps(
{'scmurl': 'git://pkgs.stg.fedoraproject.org/modules/'
'testmodule.git?#68932c90de214d9d13feefbd35246a81b6cb8d49'}))
data = json.loads(rv.data)
self.assertEquals(data['component_builds'], [61])
self.assertEquals(data['name'], 'fakemodule')
self.assertEquals(data['scmurl'],
('git://pkgs.stg.fedoraproject.org/modules/testmodule'
'.git?#68932c90de214d9d13feefbd35246a81b6cb8d49'))
self.assertEquals(data['release'], '5')
self.assertTrue(data['time_submitted'] is not None)
self.assertTrue(data['time_modified'] is not None)
self.assertEquals(data['release'], '5')
self.assertEquals(data['time_completed'], None)
self.assertEquals(data['version'], '4.3.44')
self.assertEquals(data['owner'], 'Homer J. Simpson')
self.assertEquals(data['id'], 31)
self.assertEquals(data['state_name'], 'wait')
@patch('rida.auth.is_packager', return_value=None)
def test_submit_build_cert_error(self, mocked_is_packager):
rv = self.client.post('/rida/module-builds/', data=json.dumps(
{'scmurl': 'git://pkgs.stg.fedoraproject.org/modules/'
'testmodule.git?#48932b90de214d9d13feefbd35246a81b6cb8d49'}))
data = json.loads(rv.data)
self.assertEquals(
data['message'],
'You must use your Fedora certificate when submitting a new build'
)
self.assertEquals(data['status'], 401)
self.assertEquals(data['error'], 'Unauthorized')
@patch('rida.auth.is_packager', return_value='Homer J. Simpson')
def test_submit_build_scm_url_error(self, mocked_is_packager):
rv = self.client.post('/rida/module-builds/', data=json.dumps(
{'scmurl': 'git://badurl.com'}))
data = json.loads(rv.data)
self.assertEquals(
data['message'], 'The submitted scmurl isn\'t allowed')
self.assertEquals(data['status'], 401)
self.assertEquals(data['error'], 'Unauthorized')
@patch('rida.auth.is_packager', return_value='Homer J. Simpson')
@patch('rida.scm.SCM')
def test_submit_build_bad_modulemd(self, mocked_scm, mocked_is_packager):
rv = self.client.post('/rida/module-builds/', data=json.dumps(
{'scmurl': 'git://badurl.com'}))
def mocked_scm_checkout(temp_dir):
scm_dir = path.join(temp_dir, 'fakemodule')
mkdir(scm_dir)
base_dir = path.abspath(path.dirname(__file__))
with open(path.join(scm_dir, 'fakemodule.yaml'), 'w+') as file:
file.write('Bad YAML')
file.write('Some more bad YAML for good luck')
return scm_dir
mocked_scm.return_value.checkout = mocked_scm_checkout
mocked_scm.return_value.name = 'fakemodule'
rv = self.client.post('/rida/module-builds/', data=json.dumps(
{'scmurl': 'git://pkgs.stg.fedoraproject.org/modules/'
'testmodule.git?#68932c90de214d9d13feefbd35246a81b6cb8d49'}))
data = json.loads(rv.data)
self.assertEquals(
data['message'], 'Invalid modulemd')
self.assertEquals(data['status'], 422)
self.assertEquals(data['error'], 'Unprocessable Entity')