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

Refactor user validations #1090

Merged
merged 2 commits into from Oct 13, 2018
Merged

Conversation

AndyStabler
Copy link
Contributor

Addresses the following Rubocop issue by extracting methods for each validation around personal access tokens:

app/models/user.rb:123:3: C: Metrics/CyclomaticComplexity: Cyclomatic complexity for personal_access_token_validator is too high. [8/6]
  def personal_access_token_validator ...
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Although this reduces the cyclomatic complexity of personal_access_token_validator, it introduces 5 new methods 🤔If we want to refactor personal_access_token_validator is it worth introducing a validator class for this logic to live in?

(also, I get it if you're not bothered by the Rubocop issue and want to close this—I'm just having fun exploring the codebase 😄)

@andrew
Copy link
Member

andrew commented Oct 12, 2018

This definitely feels like a good candidate to pull out into its own class 👍

@AndyStabler
Copy link
Contributor Author

This definitely feels like a good candidate to pull out into its own class 👍

Thanks for the feedback, @andrew. I'll do some extractin' 😄

@AndyStabler AndyStabler changed the title Refactor user validations [WIP] Refactor user validations Oct 13, 2018
@AndyStabler
Copy link
Contributor Author

⚠️🕵️
This sneaky line in master isn't doing any asserting, and changing it to assert the error is included breaks the following tests:

UserTest#test_does_not_allow_a_personal_access_token_without_the_read:org_scope_if_restricted_access_enabled [/Users/andy/dev/Ruby/rails/octobox/test/models/user_test.rb:98]:
UserTest#test_does_not_allow_a_personal_access_token_without_the_notifications_scope [/Users/andy/dev/Ruby/rails/octobox/test/models/user_test.rb:90]:

They're failing because those tests have an invalid github id and the validation returns early if this is the case (see here). This means the expected errors aren't being added to the error object.

@andrew what would you like to do here? We could stop returning early in the validation and add all errors to the errors object, or keep the behaviour as-is and change these broken tests such that they get past the github id validation.

@andrew
Copy link
Member

andrew commented Oct 13, 2018

@AndyStabler ah pesky 🤔

Adding all the errors feels like the right way to go to me

Extracts methods for each validation around personal access tokens

Addresses the following Rubocop issue:

app/models/user.rb:123:3: C: Metrics/CyclomaticComplexity: Cyclomatic complexity for personal_access_token_validator is too high. [8/6]
  def personal_access_token_validator ...
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The token validation logic was quite noisy in the User class, so this
commit pulls it out into its own class.

As part of this we have updated the validation to not return early if
validation fails—now all validation errors will be added to the errors object.

class PersonalAccessTokenValidatorTest < ActiveSupport::TestCase
setup do
@user = build(:user, personal_access_token: '1234')

This comment was marked as spam.

This comment was marked as spam.

@AndyStabler
Copy link
Contributor Author

Alrighty, @andrew this is ready for review again just whenever you get the time 🙇

@AndyStabler AndyStabler changed the title [WIP] Refactor user validations Refactor user validations Oct 13, 2018
@andrew
Copy link
Member

andrew commented Oct 13, 2018

Great work, a very nice refactoring 👌

@andrew andrew merged commit 951068b into octobox:master Oct 13, 2018
coreyja added a commit to coreyja/octobox that referenced this pull request Oct 16, 2018
* 'master' of github.com:coreyja/octobox: (56 commits)
  Add Guard gem and config (octobox#1111)
  Bump xpath from 3.1.0 to 3.2.0 (octobox#1107)
  Bump jwt from 1.5.6 to 2.1.0 (octobox#1104)
  Bump autoprefixer-rails from 9.1.4 to 9.2.0 (octobox#1103)
  Bump i18n from 1.1.0 to 1.1.1 (octobox#1102)
  Bump mail from 2.7.0 to 2.7.1 (octobox#1101)
  Refactor user validations (octobox#1090)
  Bump oauth2 from 1.4.0 to 1.4.1 (octobox#1100)
  Tiny typo fix in notification model test (octobox#1099)
  Show first few lines of issue/pr body as title on notification links (octobox#1097)
  Fix typo for Unlabelled filter (octobox#1098)
  Subject syncing is now live on Octobox.io
  Sync subject body for issues and pull requests (octobox#1094)
  Ensure locked field is updated when existing subjects are updated (octobox#1096)
  Status webhooks should update subects with combined status result (octobox#1093)
  Fix dropdown svg icons colors
  Bump octokit from 4.12.0 to 4.13.0 (octobox#1091)
  Fixed sidebar colors (octobox#1079)
  Rubocop fixes (octobox#1075)
  Prompt users to log into the GitHub App (octobox#1078)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants