def test_outdated_chroot_message(self, f_users, f_coprs, f_mock_chroots, f_db): chroots = [self.c1.copr_chroots[0], self.c2.copr_chroots[0]] for chroot in chroots: chroot.delete_after = datetime.datetime.now() + datetime.timedelta( days=7 + 1) # 7 days = 6d, 23h, 59m, ... app.config["SERVER_NAME"] = "localhost" app.config["DELETE_EOL_CHROOTS_AFTER"] = 123 with app.app_context(): msg = OutdatedChrootMessage(chroots) assert msg.subject == "[Copr] upcoming deletion of outdated chroots in your projects" assert msg.text == ( "You have been notified because you are an admin of projects, " "that have some builds in outdated chroots\n\n" "According to the 'Copr outdated chroots removal policy'\n" "https://docs.pagure.org/copr.copr/copr_outdated_chroots_removal_policy.html\n" "data are going to be preserved 123 days after the chroot is EOL " "and then automatically deleted, unless you decide to prolong the expiration period.\n\n" "Please, visit the projects settings if you want to extend the time.\n\n" "Project: user1/foocopr\n" "Chroot: fedora-18-x86_64\n" "Remaining: 7 days\n" "https://localhost/coprs/user1/foocopr/repositories/\n\n" "Project: user2/foocopr\n" "Chroot: fedora-17-x86_64\n" "Remaining: 7 days\n" "https://localhost/coprs/user2/foocopr/repositories/\n\n")
def test_unique_coprs(self): app.config["PINNED_PROJECTS_LIMIT"] = 2 with app.app_context(): form = PinnedCoprsForm() form.copr_ids.data = ["1", "1"] assert not form.validate() assert "only once" in form.errors["coprs"][0]
def test_builds(self, f_users, f_coprs, f_builds, f_db): app.config["SERVER_NAME"] = "localhost" with app.app_context(): dumper = UserDataDumper(self.u1) builds = dumper.builds assert len(builds) == 1 assert builds[0]["id"] == 1 assert builds[0]["project"] == "user1/foocopr"
def test_dumps(self, f_users, f_fas_groups, f_coprs, f_db): app.config["SERVER_NAME"] = "localhost" with app.app_context(): dumper = UserDataDumper(self.u1) output = dumper.dumps() assert type(output) == str data = json.loads(output) assert "username" in data assert "projects" in data
def test_data(self, f_users, f_fas_groups, f_coprs, f_db): app.config["SERVER_NAME"] = "localhost" with app.app_context(): dumper = UserDataDumper(self.u1) data = dumper.data assert "username" in data assert type(data["groups"]) == list assert type(data["projects"]) == list assert type(data["builds"]) == list
def test_limit(self): app.config["PINNED_PROJECTS_LIMIT"] = 1 with app.app_context(): form = PinnedCoprsForm() form.copr_ids.data = ["1"] assert form.validate() form.copr_ids.data = ["1", "2"] assert not form.validate() assert "Too many" in form.errors["coprs"][0]
def create_sqlite_file_function(): with app.app_context(): uri = app.config["SQLALCHEMY_DATABASE_URI"] if not uri.startswith("sqlite"): return None # strip sqlite:/// datadir_name = os.path.dirname(uri[10:]) if not os.path.exists(datadir_name): os.makedirs(datadir_name)
def test_fork_copr(self, f_users, f_coprs, f_mock_chroots, f_builds, f_db): with app.app_context(): with mock.patch('flask.g') as mc_flask_g: mc_flask_g.user.name = self.u2.name forking = ProjectForking(self.u1) fc1 = forking.fork_copr(self.c1, "new-name") assert fc1.id != self.c1.id assert fc1.name == "new-name" assert fc1.forked_from_id == self.c1.id assert fc1.mock_chroots == self.c1.mock_chroots
def test_repo_renders_http(self, f_users, f_coprs, f_mock_chroots, f_db): url = "/coprs/{user}/{copr}/repo/{chroot}/{user}-{copr}-{chroot}.repo".format( user = self.u1.username, copr = self.c1.name, chroot = "{}-{}".format(self.mc1.os_release, self.mc1.os_version), ) app.config["REPO_NO_SSL"] = True app.config["ENFORCE_PROTOCOL_FOR_BACKEND_URL"] = "https" with app.app_context(): res = self.tc.get(url) assert res.status_code == 200 assert 'baseurl=http://' in res.data.decode('utf-8')
def test_legal_flag_message(self, f_users, f_coprs, f_db): app.config["SERVER_NAME"] = "localhost" with app.app_context(): msg = LegalFlagMessage( self.c1, self.u2, "There are forbidden packages in the project") assert msg.subject == "Legal flag raised on foocopr" assert msg.text == ( "There are forbidden packages in the project\n" "Navigate to http://localhost/admin/legal-flag/\n" "Contact on owner is: user1 <*****@*****.**>\n" "Reported by user2 <*****@*****.**>")
def test_fork_copr_sends_actions(self, f_users, f_coprs, f_mock_chroots, f_builds, f_db): with app.app_context(): with mock.patch('flask.g') as mc_flask_g: mc_flask_g.user.name = self.u2.name fc1, created = ComplexLogic.fork_copr(self.c1, self.u2, u"dstname") self.db.session.commit() actions = ActionsLogic.get_many(ActionTypeEnum("fork")).all() assert len(actions) == 1 data = json.loads(actions[0].data) assert data["user"] == self.u2.name assert data["copr"] == "dstname" assert data["builds_map"] == {'srpm-builds': {'bar': '00000005'},'fedora-18-x86_64': {'bar': '00000005-hello-world'}}
def test_chroot_alias(self, f_users, f_coprs, f_mock_chroots, f_db): # Test a chroot alias feature on a real-world example (RhBug: 1756632) mc_kwargs = dict(os_version="8", arch="x86_64", is_active=True, distgit_branch=models.DistGitBranch(name="bar")) mc_epel = models.MockChroot(os_release="epel", **mc_kwargs) mc_rhelbeta = models.MockChroot(os_release="rhelbeta", **mc_kwargs) cc_epel = models.CoprChroot(mock_chroot=mc_epel) cc_rhelbeta = models.CoprChroot(mock_chroot=mc_rhelbeta) self.c1.copr_chroots = [cc_epel, cc_rhelbeta] self.db.session.commit() app.config["BACKEND_BASE_URL"] = "https://foo" with app.app_context(): kwargs = dict(user=self.u1.username, copr=self.c1.name) url = "/coprs/{user}/{copr}/repo/{chroot}/" # Both chroots enabled, without alias r1 = self.tc.get(url.format(chroot="epel-8", **kwargs)) r2 = self.tc.get(url.format(chroot="rhelbeta-8", **kwargs)) assert "baseurl=https://foo/results/user1/foocopr/epel-8-$basearch/" in r1.data.decode( "utf-8") assert "baseurl=https://foo/results/user1/foocopr/rhelbeta-8-$basearch/" in r2.data.decode( "utf-8") # Both chroots enabled, alias defined app.config["CHROOT_NAME_RELEASE_ALIAS"] = {"epel-8": "rhelbeta-8"} r1 = self.tc.get(url.format(chroot="epel-8", **kwargs)) r2 = self.tc.get(url.format(chroot="rhelbeta-8", **kwargs)) assert "baseurl=https://foo/results/user1/foocopr/epel-8-$basearch/" in r1.data.decode( "utf-8") assert "baseurl=https://foo/results/user1/foocopr/rhelbeta-8-$basearch/" in r2.data.decode( "utf-8") # Only one chroot enabled, alias defined self.c1.copr_chroots = [cc_rhelbeta] self.db.session.commit() cache.clear() r1 = self.tc.get(url.format(chroot="epel-8", **kwargs)) r2 = self.tc.get(url.format(chroot="rhelbeta-8", **kwargs)) assert "baseurl=https://foo/results/user1/foocopr/rhelbeta-8-$basearch/" in r1.data.decode( "utf-8") assert "baseurl=https://foo/results/user1/foocopr/rhelbeta-8-$basearch/" in r2.data.decode( "utf-8")
def test_pre_process_repo_url(self): app = Flask(__name__) app.config["BACKEND_BASE_URL"] = "http://backend" test_cases = [ ("http://example1.com/foo/$chroot/", "http://example1.com/foo/fedora-rawhide-x86_64/"), ("copr://someuser/someproject", "http://backend/results/someuser/someproject/fedora-rawhide-x86_64/"), ("copr://someuser/someproject?foo=bar&baz=10", "http://backend/results/someuser/someproject/fedora-rawhide-x86_64/"), ("http://example1.com/foo/$chroot?priority=10", "http://example1.com/foo/fedora-rawhide-x86_64"), ("http://example1.com/foo/$chroot?priority=10&foo=bar", "http://example1.com/foo/fedora-rawhide-x86_64?foo=bar"), ] with app.app_context(): for url, exp in test_cases: assert pre_process_repo_url("fedora-rawhide-x86_64", url) == exp
def test_notify_outdated_chroots(self, send_mail, dev_instance_warning, f_users, f_coprs, f_mock_chroots, f_db): app.config["SERVER_NAME"] = "localhost" with app.app_context(): # Any copr chroots are marked to be deleted, hence there is nothing to be notified about notify_outdated_chroots_function(dry_run=False, email_filter=None, all=False) assert send_mail.call_count == 0 # Mark a copr chroot to be deleted, we should send a notification self.c2.copr_chroots[0].delete_after = datetime.today( ) + timedelta(days=150) assert self.c2.copr_chroots[0].delete_notify is None notify_outdated_chroots_function(dry_run=False, email_filter=None, all=False) assert self.c2.copr_chroots[0].delete_notify is not None assert send_mail.call_count == 1 recipients, message = send_mail.call_args[0] assert isinstance(message, OutdatedChrootMessage) assert recipients == ["*****@*****.**"] assert "Project: user2/foocopr" assert "Chroot: fedora-17-x86_64" assert "Remaining: 149 days" # Run notifications immediately once more # Nothing should change, we have a mechanism to not spam users previous_delete_notify = self.c2.copr_chroots[0].delete_notify notify_outdated_chroots_function(dry_run=False, email_filter=None, all=False) assert send_mail.call_count == 1 # No new calls assert self.c2.copr_chroots[ 0].delete_notify == previous_delete_notify # Now, don't care when we sent last notifications. Notify everyone again notify_outdated_chroots_function(dry_run=False, email_filter=None, all=True) assert send_mail.call_count == 2 assert self.c2.copr_chroots[ 0].delete_notify != previous_delete_notify
def test_projects(self, f_users, f_coprs, f_db): app.config["SERVER_NAME"] = "localhost" with app.app_context(): dumper = UserDataDumper(self.u1) projects = dumper.projects assert [p["full_name"] for p in projects] == ["user1/foocopr"]
def notify_outdated_chroots(dry_run, email_filter, all): with app.app_context(): return notify_outdated_chroots_function(dry_run, email_filter, all)