Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a fix for duplicate alias problem #3010

Merged
merged 22 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/auslib/web/admin/views/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ def _post(self, transaction, changed_by):
if what.get("fallbackMapping") is not None and len(fallback_mapping_values) != 1:
return problem(400, "Bad Request", "Invalid fallbackMapping value. No release name found in DB")

alias = what.get("alias", None)
if alias is not None and dbo.rules.getRule(alias):
return problem(400, "Bad Request", "Rule with alias exists.")

if alias is not None:
scheduled_rule_with_alias = dbo.rules.scheduled_changes.t.select().where(dbo.rules.scheduled_changes.base_alias == alias).execute().fetchall()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This (and the similar block below) are missing the conditions I called out in #3010 (comment):

  • complete is False
  • change_type is insert or update

if len(scheduled_rule_with_alias) > 0:
return problem(400, "Bad Request", "Rule is scheduled with the given alias.")

return super(RuleScheduledChangesView, self)._post(what, transaction, changed_by, change_type)


Expand Down Expand Up @@ -318,6 +327,15 @@ def _post(self, sc_id, transaction, changed_by):
if what.get("fallbackMapping") is not None and len(fallback_mapping_values) != 1:
return problem(400, "Bad Request", "Invalid fallbackMapping value. No release name found in DB")

alias = what.get("alias", None)
if alias is not None and dbo.rules.getRule(alias):
return problem(400, "Bad Request", "Rule with alias exists.")

if alias is not None:
scheduled_rule_with_alias = dbo.rules.scheduled_changes.t.select().where(dbo.rules.scheduled_changes.base_alias == alias).execute().fetchall()
if len(scheduled_rule_with_alias) > 0:
return problem(400, "Bad Request", "rule is scheduled with the given alias.")

return super(RuleScheduledChangeView, self)._post(sc_id, what, transaction, changed_by, connexion.request.get_json().get("sc_data_version", None))

@requirelogin
Expand Down
118 changes: 118 additions & 0 deletions tests/admin/views/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,124 @@ def testAddScheduledChangeMultipleConditions(self):
ret = self._post("scheduled_changes/rules", data=data)
self.assertEqual(ret.status_code, 400)

@mock.patch("time.time", mock.MagicMock(return_value=300))
def testAddScheduledChangesRuleWithDuplicateAliasWithChangeTypeInsert(self):
ret = self._post(
"/rules", data=dict(backgroundRate=31, mapping="c", priority=33, product="Firefox", update_type="minor", channel="nightly", alias="test")
)
self.assertEqual(ret.status_code, 200, "Status Code: %d, Data: %s" % (ret.status_code, ret.get_data()))

data1 = {
"when": 1234567,
"priority": 120,
"backgroundRate": 100,
"product": "blah",
"channel": "blah",
"mapping": "a",
"change_type": "insert",
"base_alias": "test",
}
ret1 = self._post("/scheduled_changes/rules", data=data1)
godplayer56 marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(ret1.status_code, 400, ret1.get_data())

@mock.patch("time.time", mock.MagicMock(return_value=300))
def testAddScheduledChangesRuleWithDuplicateAliasWithChangeTypeUpdate(self):
ret = self._post(
"/rules", data=dict(backgroundRate=31, mapping="c", priority=33, product="Firefox", update_type="minor", channel="nightly", alias="test")
)
self.assertEqual(ret.status_code, 200, "Status Code: %d, Data: %s" % (ret.status_code, ret.get_data()))

data1 = {
"when": 1234567,
"priority": 120,
"backgroundRate": 100,
"product": "blah",
"channel": "blah",
"mapping": "a",
"change_type": "update",
"base_alias": "test",
}
ret1 = self._post("/scheduled_changes/rules", data=data1)
self.assertEqual(ret1.status_code, 400, ret1.get_data())

@mock.patch("time.time", mock.MagicMock(return_value=300))
def testAddScheduledChangeWithAliasAlreadyPresentWithChangeTypeInsert(self):
data = {
"rule_id": 5,
"telemetry_product": None,
"telemetry_channel": None,
"telemetry_uptake": None,
"priority": 80,
"buildTarget": "d",
"version": "3.3",
"backgroundRate": 100,
"mapping": "c",
"update_type": "minor",
"data_version": 1,
"change_type": "insert",
"when": 1234567,
"base_alias": "test",
"complete": False,
}
ret = self._post("/scheduled_changes/rules", data=data)
self.assertEqual(ret.status_code, 200, ret.get_data())

data1 = {
"when": 2000000,
"data_version": 1,
"rule_id": 1,
"priority": 100,
"version": "3.5",
"buildTarget": "d",
"backgroundRate": 100,
"mapping": "c",
"update_type": "minor",
"sc_data_version": 1,
"base_alias": "test",
"complete": False,
}
ret1 = self._post("/scheduled_changes/rules/4", data=data1)
self.assertEqual(ret1.status_code, 400, ret1.get_data())

@mock.patch("time.time", mock.MagicMock(return_value=300))
def testAddScheduledChangeWithAliasAlreadyPresentWithChangeTypeUpdate(self):
data = {
"rule_id": 5,
"telemetry_product": None,
"telemetry_channel": None,
"telemetry_uptake": None,
"priority": 80,
"buildTarget": "d",
"version": "3.3",
"backgroundRate": 100,
"mapping": "c",
"update_type": "minor",
"data_version": 1,
"change_type": "update",
"when": 1234567,
"base_alias": "test",
"complete": False,
}
ret = self._post("/scheduled_changes/rules", data=data)
self.assertEqual(ret.status_code, 200, ret.get_data())

data1 = {
"when": 2000000,
"data_version": 1,
"rule_id": 1,
"priority": 100,
"version": "3.5",
"buildTarget": "d",
"backgroundRate": 100,
"mapping": "c",
"update_type": "minor",
"sc_data_version": 1,
"base_alias": "test",
"complete": False,
}
ret1 = self._post("/scheduled_changes/rules/4", data=data1)
self.assertEqual(ret1.status_code, 400, ret1.get_data())

def testAddScheduledChangeMissingRequiredTelemetryFields(self):
data = {"telemetry_product": "foo", "priority": 120, "backgroundRate": 100, "update_type": "minor", "change_type": "insert"}
ret = self._post("scheduled_changes/rules", data=data)
Expand Down