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 5 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
9 changes: 9 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,10 @@ 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.")

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


Expand Down Expand Up @@ -318,6 +322,11 @@ 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")

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

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

@requirelogin
Expand Down
11 changes: 11 additions & 0 deletions tests/admin/views/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -2834,3 +2834,14 @@ def testCannotRevokeOtherUsersSignoffAsNormalUser(self):
ret = self._delete("/scheduled_changes/rules/3/signoffs", username="julie", qs={"username": "mary"})
self.assertEqual(ret.status_code, 403, ret.get_data())
self.assertEqual(ret.mimetype, "application/json")

def testScheduledChangeWithDuplicateAlias(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

This test is not quite right, unfortunately. It is testing the /rules view - which is already covered by

ret = self._post(
.

You should be adding at least two tests:

In both cases the request should a result in a 400 if any of the following are true:

  • The alias exists in any row in the rules table
  • The alias exists in any row in the rule_scheduled_changes table that meets both the following conditions:
    • complete is False
    • change_type is insert or update

(This may require additional changes to rules.py as well - please let me know if you have any questions.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll start working on updating the test 👍

Copy link
Contributor Author

@godplayer56 godplayer56 Oct 18, 2023

Choose a reason for hiding this comment

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

@bhearsum I have a doubt regarding this -
How do I select the required data in the rule_scheduled_change table? for eg-
for the rules table we have the getAlias function defined to get the rule with the given alias but for rule_scheduled_changes I am not able to figure out how should I select the data with a given alias.

and also does the column scheduled_by denote the alias for the rule_scheduled_changes table?

Copy link
Contributor

Choose a reason for hiding this comment

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

@bhearsum I have a doubt regarding this - How do I select the required data in the rule_scheduled_change table? for eg- for the rules table we have the getAlias function defined to get the rule with the given alias but for rule_scheduled_changes I am not able to figure out how should I select the data with a given alias.

The scheduled_changes table is accessible through dbo.rules.scheduled_changes (You can see an example query in the tests

r = dbo.rules.scheduled_changes.t.select().where(dbo.rules.scheduled_changes.sc_id == 8).execute().fetchall()
- and there's many more example of selects in other tests and parts of the codebase).

and also does the column scheduled_by denote the alias for the rule_scheduled_changes table?

No -- scheduled_by is the person who scheduled the change :). The alias is in the base_alias column in the scheduled changes table (apologies -- I forgot about that detail when I posted the earlier comment).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok got it! 😄

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()))

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, 400, "Status Code: %d, Data: %s" % (ret.status_code, ret.get_data()))