Skip to content

Commit

Permalink
Merge pull request #159 from canonical/revert-152-order-by-ascending
Browse files Browse the repository at this point in the history
Revert "Rework notices query"
  • Loading branch information
mtruj013 authored Jun 25, 2024
2 parents 6b1384e + e345485 commit a93d5e4
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 178 deletions.
2 changes: 1 addition & 1 deletion migrations/versions/c27fec1bfee2_.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index("notices_published_idx", table_name="notice")
op.drop_index("notices_published_desc_idx", table_name="notice")
# ### end Alembic commands ###
# ### end Alembic commands ###
23 changes: 11 additions & 12 deletions tests/fixtures/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ def make_models():
debian="test-package-debian",
)

notice = Notice(
id="USN-1111-01",
is_hidden=False,
published=datetime.now(),
summary="",
details="",
instructions="",
releases=[release],
)
notice.cves = []

cve = CVE(
id="CVE-1111-0001",
published=datetime.now(),
Expand All @@ -50,7 +39,6 @@ def make_models():
tags={},
bugs={},
status="active",
notices=[notice],
)

status = Status(
Expand All @@ -60,6 +48,17 @@ def make_models():
release=release,
)

notice = Notice(
id="USN-1111-01",
is_hidden=False,
published=datetime.now(),
summary="",
details="",
instructions="",
releases=[release],
cves=[cve],
)

return {
"release": release,
"package": package,
Expand Down
83 changes: 11 additions & 72 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,6 @@ def test_cves(self):
assert len(response.json["cves"]) == 1
assert response.json["cves"][0]["id"] == "CVE-1111-0001"

# Should include the expected notice for this cve
assert (
response.json["cves"][0]["notices"][0]["id"]
== self.models["notice"].id
)
assert response.json["cves"][0]["notices"][0]["published"] == str(
self.models["notice"].published.isoformat()
)

def test_cve_not_exists(self):
response = self.client.get("/security/cves/CVE-0000-0000.json")

Expand Down Expand Up @@ -944,35 +935,6 @@ def test_usn(self):
assert response.status_code == 200
assert response.json["cves_ids"] == self.models["notice"].cves_ids

def test_usn_with_multiple_cves(self):
# Create additional cves
response_3 = self.client.put(
"/security/cves.json",
json=[
payloads.cve3,
payloads.cve4,
],
)
assert response_3.status_code == 200

notice = payloads.notice.copy()

notice["cves"] = [payloads.cve3["id"], payloads.cve4["id"]]

response = self.client.post("/security/notices.json", json=notice)

assert response.status_code == 200

response = self.client.get(f"/security/notices/{notice['id']}.json")

assert response.status_code == 200
assert sorted(response.json["cves_ids"]) == sorted(
[
payloads.cve3["id"],
payloads.cve4["id"],
]
)

def test_usns_returns_200_for_non_existing_release(self):
response = self.client.get("/security/notices.json?release=no-exist")

Expand All @@ -982,30 +944,9 @@ def test_usns_returns_200_for_non_existing_release(self):
)

def test_create_usn(self):
notice = payloads.notice.copy()

notice["cves"] = [self.models["cve"].id]

response = self.client.post("/security/notices.json", json=notice)

assert response.status_code == 200

def test_create_usn_with_cves(self):
# Create additional cves
response_3 = self.client.put(
"/security/cves.json",
json=[
payloads.cve3,
payloads.cve4,
],
response = self.client.post(
"/security/notices.json", json=payloads.notice
)
assert response_3.status_code == 200

notice = payloads.notice.copy()

notice["cves"] = [payloads.cve3["id"], payloads.cve4["id"]]

response = self.client.post("/security/notices.json", json=notice)

assert response.status_code == 200

Expand All @@ -1017,15 +958,16 @@ def test_create_ssn_usn(self):
assert response.status_code == 200

def test_create_usn_returns_422_for_non_unique_id(self):
notice = payloads.notice.copy()
notice["cves"] = [self.models["cve"].id]

# Create USN
response_1 = self.client.post("/security/notices.json", json=notice)
response_1 = self.client.post(
"/security/notices.json", json=payloads.notice
)
assert response_1.status_code == 200

# Create again
response_2 = self.client.post("/security/notices.json", json=notice)
response_2 = self.client.post(
"/security/notices.json", json=payloads.notice
)
assert response_2.status_code == 422
assert (
f"'{payloads.notice['id']}' already exists"
Expand All @@ -1046,8 +988,6 @@ def test_update_usn(self):

# Create first
notice = payloads.notice.copy()
notice["cves"] = [self.models["cve"].id]

response_1 = self.client.post("/security/notices.json", json=notice)
assert response_1.status_code == 200

Expand Down Expand Up @@ -1090,10 +1030,9 @@ def test_delete_usn_returns_404_for_non_existing_usn(self):

def test_delete_usn(self):
# Create USN first
notice = payloads.notice.copy()
notice["cves"] = [self.models["cve"].id]

response = self.client.post("/security/notices.json", json=notice)
response = self.client.post(
"/security/notices.json", json=payloads.notice
)
assert response.status_code == 200

# Now delete it
Expand Down
10 changes: 4 additions & 6 deletions webapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
Enum,
Float,
ForeignKey,
Index,
JSON,
String,
Table,
Expand Down Expand Up @@ -82,7 +81,9 @@ class CVE(db.Model):
Enum("not-in-ubuntu", "active", "rejected", name="cve_statuses")
)
statuses = relationship("Status", cascade="all, delete-orphan")
notices = relationship("Notice", secondary=notice_cves)
notices = relationship(
"Notice", secondary=notice_cves, back_populates="cves"
)

@hybrid_method
def get_filtered_notices(self, show_hidden=False):
Expand Down Expand Up @@ -152,6 +153,7 @@ class Notice(db.Model):
details = Column(String)
instructions = Column(String)
release_packages = Column(JSON)
cves = relationship("CVE", secondary=notice_cves, back_populates="notices")
references = Column(JSON)
is_hidden = Column(Boolean, nullable=False)
releases = relationship(
Expand All @@ -160,10 +162,6 @@ class Notice(db.Model):
order_by="desc(Release.release_date)",
back_populates="notices",
)
notices_published_idx = Index("notices_published_idx", published)
notices_published_desc_idx = Index(
"notices_published_desc_idx", published.desc()
)

@hybrid_property
def cves_ids(self):
Expand Down
Loading

0 comments on commit a93d5e4

Please sign in to comment.