From d895b4d5ff1294731caa53e6898cfc949ee36f7a Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Mon, 24 Apr 2017 23:08:28 -0400 Subject: [PATCH] Fix appr tests to use the shared test fixtures --- endpoints/appr/test/test_api.py | 87 ++---------------------- endpoints/appr/test/test_api_security.py | 2 +- endpoints/appr/test/test_registry.py | 2 +- initdb.py | 3 + test/fixtures.py | 4 ++ test/test_api_usage.py | 8 ++- 6 files changed, 20 insertions(+), 86 deletions(-) diff --git a/endpoints/appr/test/test_api.py b/endpoints/appr/test/test_api.py index 025f8d747..853d7f77f 100644 --- a/endpoints/appr/test/test_api.py +++ b/endpoints/appr/test/test_api.py @@ -1,28 +1,22 @@ -import os -import shutil import uuid import pytest -from cnr.models.db_base import CnrDB + from cnr.tests.conftest import * from cnr.tests.test_apiserver import BaseTestServer from cnr.tests.test_models import CnrTestModels -from peewee import SqliteDatabase import data.oci_model.blob as oci_blob -from app import app as application -from data.database import db as database -from data.database import User, close_db_filter + +from data.database import User from data.interfaces.appr import oci_app_model from data.model import organization, user -from endpoints.appr import appr_bp, registry +from endpoints.appr import registry # Needed to register the endpoint from endpoints.appr.cnr_backend import Channel, Package, QuayDB -from initdb import initialize_database, populate_database, wipe_database -application.register_blueprint(appr_bp, url_prefix='/cnr') +from test.fixtures import * -# TODO: avoid direct usage of database def create_org(namespace, owner): try: User.get(username=namespace) @@ -75,82 +69,17 @@ class PackageTest(Package): @pytest.fixture(autouse=True) -def quaydb(monkeypatch): +def quaydb(monkeypatch, app): monkeypatch.setattr('endpoints.appr.cnr_backend.QuayDB.Package', PackageTest) monkeypatch.setattr('endpoints.appr.cnr_backend.Package', PackageTest) monkeypatch.setattr('endpoints.appr.registry.Package', PackageTest) monkeypatch.setattr('cnr.models.Package', PackageTest) monkeypatch.setattr('endpoints.appr.cnr_backend.QuayDB.Channel', ChannelTest) - # monkeypatch.setattr('data.cnrmodel.channel.Channel', ChannelTest) monkeypatch.setattr('endpoints.appr.registry.Channel', ChannelTest) monkeypatch.setattr('cnr.models.Channel', ChannelTest) -def seed_db(): - create_org("titi", user.get_user("devtable")) - - -@pytest.fixture() -def sqlitedb_file(tmpdir): - test_db_file = tmpdir.mkdir("quaydb").join("test.db") - return str(test_db_file) - - -@pytest.fixture(scope="module") -def init_db_path(tmpdir_factory): - sqlitedb_file_loc = str(tmpdir_factory.mktemp("data").join("test.db")) - sqlitedb = 'sqlite:///{0}'.format(sqlitedb_file_loc) - conf = {"TESTING": True, "DEBUG": True, "DB_URI": sqlitedb} - os.environ['TEST_DATABASE_URI'] = str(sqlitedb) - os.environ['DB_URI'] = str(sqlitedb) - database.initialize(SqliteDatabase(sqlitedb_file_loc)) - application.config.update(conf) - application.config.update({"DB_URI": sqlitedb}) - wipe_database() - initialize_database() - populate_database(minimal=True) - close_db_filter(None) - seed_db() - return str(sqlitedb_file_loc) - - -@pytest.fixture() -def database_uri(monkeypatch, init_db_path, sqlitedb_file): - shutil.copy2(init_db_path, sqlitedb_file) - database.initialize(SqliteDatabase(sqlitedb_file)) - db_path = 'sqlite:///{0}'.format(sqlitedb_file) - monkeypatch.setenv("DB_URI", db_path) - seed_db() - return db_path - - -@pytest.fixture() -def appconfig(database_uri): - conf = {"TESTING": True, "DEBUG": True, "DB_URI": database_uri} - return conf - - -@pytest.fixture() -def create_app(): - try: - application.register_blueprint(appr_bp, url_prefix='') - except: - pass - return application - - -@pytest.fixture(autouse=True) -def app(create_app, appconfig): - create_app.config.update(appconfig) - return create_app - - -@pytest.fixture() -def db(): - return CnrDB - - class TestServerQuayDB(BaseTestServer): DB_CLASS = QuayDB @@ -180,10 +109,6 @@ class TestServerQuayDB(BaseTestServer): class TestQuayModels(CnrTestModels): DB_CLASS = QuayDB - @pytest.fixture(autouse=True) - def load_db(self, appconfig): - return appconfig - @pytest.mark.xfail def test_channel_delete_releases(self, db_with_data1): """ Can't remove a release from the channel, only delete the channel entirely """ diff --git a/endpoints/appr/test/test_api_security.py b/endpoints/appr/test/test_api_security.py index f87b4a5d3..6c9c3384b 100644 --- a/endpoints/appr/test/test_api_security.py +++ b/endpoints/appr/test/test_api_security.py @@ -6,7 +6,7 @@ from flask import url_for from data import model from endpoints.appr.registry import appr_bp, blobs from endpoints.api.test.shared import client_with_identity -from test.fixtures import app, appconfig, database_uri, init_db_path, sqlitedb_file +from test.fixtures import * BLOB_ARGS = {'digest': 'abcd1235'} PACKAGE_ARGS = {'release': 'r', 'media_type': 'foo'} diff --git a/endpoints/appr/test/test_registry.py b/endpoints/appr/test/test_registry.py index 6a3037e6c..86cd6e007 100644 --- a/endpoints/appr/test/test_registry.py +++ b/endpoints/appr/test/test_registry.py @@ -30,7 +30,7 @@ def test_login(login_data, expected_code, app, client): if "+" in login_data['username'] and login_data['password'] is None: username, robotname = login_data['username'].split("+") _, login_data['password'] = model.user.create_robot(robotname, model.user.get_user(username)) - app.register_blueprint(appr_bp, url_prefix='/cnr') + url = url_for('appr.login') headers = {'Content-Type': 'application/json'} data = {'user': login_data} diff --git a/initdb.py b/initdb.py index 61491973a..7014d0965 100644 --- a/initdb.py +++ b/initdb.py @@ -672,6 +672,9 @@ def populate_database(minimal=False, with_storage=False): liborg = model.organization.create_organization('library', 'quay+library@devtable.com', new_user_1) liborg.save() + titiorg = model.organization.create_organization('titi', 'quay+titi@devtable.com', new_user_1) + titiorg.save() + thirdorg = model.organization.create_organization('sellnsmall', 'quay+sell@devtable.com', new_user_1) thirdorg.save() diff --git a/test/fixtures.py b/test/fixtures.py index 31e0f516e..174dc8a16 100644 --- a/test/fixtures.py +++ b/test/fixtures.py @@ -13,6 +13,7 @@ from data import model from data.database import close_db_filter, db, configure from data.model.user import LoginWrappedDBUser from endpoints.api import api_bp +from endpoints.appr import appr_bp from endpoints.web import web from initdb import initialize_database, populate_database @@ -158,7 +159,10 @@ def app(appconfig, initialized_db): app.url_map.converters['regex'] = RegexConverter app.url_map.converters['apirepopath'] = APIRepositoryPathConverter app.url_map.converters['repopath'] = RepositoryPathConverter + app.register_blueprint(api_bp, url_prefix='/api') + app.register_blueprint(appr_bp, url_prefix='/cnr') app.register_blueprint(web, url_prefix='/') + app.config.update(appconfig) return app diff --git a/test/test_api_usage.py b/test/test_api_usage.py index c8b7230fa..14316d41e 100644 --- a/test/test_api_usage.py +++ b/test/test_api_usage.py @@ -859,15 +859,17 @@ class TestDeleteNamespace(ApiTestCase): def test_deletenamespaces(self): self.login(ADMIN_ACCESS_USER) - # Try to first delete the user. Since they are the sole admin of two orgs, it should fail. + # Try to first delete the user. Since they are the sole admin of three orgs, it should fail. with check_transitive_modifications(): self.deleteResponse(User, expected_code=400) - # Delete the two orgs, checking in between. + # Delete the three orgs, checking in between. with check_transitive_modifications(): self.deleteEmptyResponse(Organization, params=dict(orgname=ORGANIZATION), expected_code=204) self.deleteResponse(User, expected_code=400) # Should still fail. self.deleteEmptyResponse(Organization, params=dict(orgname='library'), expected_code=204) + self.deleteResponse(User, expected_code=400) # Should still fail. + self.deleteEmptyResponse(Organization, params=dict(orgname='titi'), expected_code=204) # Add some queue items for the user. notification_queue.put([ADMIN_ACCESS_USER, 'somerepo', 'somename'], '{}') @@ -1007,7 +1009,7 @@ class TestConductSearch(ApiTestCase): json = self.getJsonResponse(ConductSearch, params=dict(query='owners')) - self.assertEquals(3, len(json['results'])) + self.assertEquals(4, len(json['results'])) self.assertEquals(json['results'][0]['kind'], 'team') self.assertEquals(json['results'][0]['name'], 'owners')