Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix RSpec matchers along with a few other things. (#420)
This commit fixes issues with the RSpec matchers, notably: - For the `except` matcher code path, `model_class.default_ignored_attributes` was being called, but that method was made protected in a previous change as discussed [here](8f8495d#commitcomment-28001384). This commit makes that method public again. - Both the `on` and `except` matcher options have undergone a pretty substantial refactor. As I was fixing the original issue with `on` and `except`, I started to notice other things and addressed them. I will try to make explanatory comments on the PR to give some insight. - The matchers as a whole have undergone pretty significant changes, and I found that many of them were not working as intended, or, for example, in the case of `on`, it was a no-op. I made the changes that I thought reflected what the matchers ought to do and I also included tests (which actually ended up being extremely useful in discovering some of the bugs!) to make sure that future changes don't break anything. - The `requires_comment` matcher's behavior has changed to look for callbacks instead. I also changed the way that comment-required state validity is checked in the `Auditor`, but I will discuss that below. My reasoning for the two changes are related though - given an audited model where audit comments are required, we only care about the existence of the comment when an object is being created, if it has been changed (and subsequently updated), or if it is about to be destroyed (and of course we can include & exclude any combination of these three states with the `on` option). Previously, the presence of the comment was related to the overall state of the object itself, and I found this confusing since the comment only matters when we are _doing_ something to the object. - I have decoupled the codepaths for each option so that they could be run independently of one another (and all together, of course). I was getting some interesting behavior before, but I believe I have nailed down the correct behavior. - I will discuss anything not discussed above for the matchers in PR comments. The `Auditor` itself: - Changed the way non-audited columns were calculated. Will discuss in-line. - Revised and (hopefully) simplified the way that audit comments are required & validated. Will discuss in-line. Other comments: - As I mentioned above, I noticed some behavioral issues that I tried to address. I will talk about them more in-line and if there are any questions about particular things I did please feel free to ask. - Another thing I would like to do is a complete rework of the test suite itself (or if anyone else wants to take this up, feel free :P) In writing the tests for the matchers, I noticed many things such as audited columns for the `User` model being inconsistent, the `Audited.ignored_attributes` changing, and more. This is directly caused by the behavior of some of the tests, and I think it would be beneficial to figure out a way to rework the behavior such that what happens in one test doesn't have an impact on all the others. I also have a slight fear that some tests may in fact be returning false-positives, but I haven't looked too far into that yet. - I was wondering if it would be better to not memoize `Auditor#audited_columns` and `Auditor#non_audited_columns` since the calculations are not really intensive enough to warrant memoization, but when I did that tests were breaking left and right and so I think this really ties into my point above about the test suite. Something to keep in mind / look into. I didn't do it in this PR because there is already quite a bit going on.
- Loading branch information
Showing
6 changed files
with
179 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require "spec_helper" | ||
|
||
describe Models::ActiveRecord::UserExceptPassword do | ||
let(:non_audited_columns) { subject.class.non_audited_columns } | ||
|
||
it { should_not be_audited.only(non_audited_columns) } | ||
it { should be_audited.except(:password) } | ||
it { should_not be_audited.requires_comment } | ||
it { should be_audited.on(:create, :update, :destroy) } | ||
# test chaining | ||
it { should be_audited.except(:password).on(:create, :update, :destroy) } | ||
end | ||
|
||
describe Models::ActiveRecord::UserOnlyPassword do | ||
let(:audited_columns) { subject.class.audited_columns } | ||
|
||
it { should be_audited.only(:password) } | ||
it { should_not be_audited.except(audited_columns) } | ||
it { should_not be_audited.requires_comment } | ||
it { should be_audited.on(:create, :update, :destroy) } | ||
it { should be_audited.only(:password).on(:create, :update, :destroy) } | ||
end | ||
|
||
describe Models::ActiveRecord::CommentRequiredUser do | ||
let(:audited_columns) { subject.class.audited_columns } | ||
let(:non_audited_columns) { subject.class.non_audited_columns } | ||
|
||
it { should_not be_audited.only(non_audited_columns) } | ||
it { should_not be_audited.except(audited_columns) } | ||
it { should be_audited.requires_comment } | ||
it { should be_audited.on(:create, :update, :destroy) } | ||
it { should be_audited.requires_comment.on(:create, :update, :destroy) } | ||
end | ||
|
||
describe Models::ActiveRecord::OnCreateCommentRequiredUser do | ||
let(:audited_columns) { subject.class.audited_columns } | ||
let(:non_audited_columns) { subject.class.non_audited_columns } | ||
|
||
it { should_not be_audited.only(non_audited_columns) } | ||
it { should_not be_audited.except(audited_columns) } | ||
it { should be_audited.requires_comment } | ||
it { should be_audited.on(:create) } | ||
it { should_not be_audited.on(:update, :destroy) } | ||
it { should be_audited.requires_comment.on(:create) } | ||
end | ||
|
||
describe Models::ActiveRecord::OnUpdateCommentRequiredUser do | ||
let(:audited_columns) { subject.class.audited_columns } | ||
let(:non_audited_columns) { subject.class.non_audited_columns } | ||
|
||
it { should_not be_audited.only(non_audited_columns) } | ||
it { should_not be_audited.except(audited_columns) } | ||
it { should be_audited.requires_comment } | ||
it { should be_audited.on(:update) } | ||
it { should_not be_audited.on(:create, :destroy) } | ||
it { should be_audited.requires_comment.on(:update) } | ||
end | ||
|
||
describe Models::ActiveRecord::OnDestroyCommentRequiredUser do | ||
let(:audited_columns) { subject.class.audited_columns } | ||
let(:non_audited_columns) { subject.class.non_audited_columns } | ||
|
||
it { should_not be_audited.only(non_audited_columns) } | ||
it { should_not be_audited.except(audited_columns) } | ||
it { should be_audited.requires_comment } | ||
it { should be_audited.on(:destroy) } | ||
it { should_not be_audited.on(:create, :update) } | ||
it { should be_audited.requires_comment.on(:destroy) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters