/
have_secure_token_matcher_spec.rb
179 lines (146 loc) · 5.94 KB
/
have_secure_token_matcher_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
require 'unit_spec_helper'
# rubocop:disable Metrics/BlockLength
describe Shoulda::Matchers::ActiveRecord::HaveSecureTokenMatcher,
type: :model do
if active_record_supports_has_secure_token?
describe '#description' do
it 'returns the message including the name of the default column' do
matcher = have_secure_token
expect(matcher.description).
to eq('have :token as a secure token')
end
it 'returns the message including the name of a provided column' do
matcher = have_secure_token(:special_token)
expect(matcher.description).
to eq('have :special_token as a secure token')
end
end
it 'matches when the subject configures has_secure_token with the db' do
create_table(:users) do |t|
t.string :token
t.index :token, unique: true
end
valid_model = define_model_class(:User) { has_secure_token }
expect(valid_model.new).to have_secure_token
end
it 'matches when the subject configures has_secure_token with the db for ' \
'a custom attribute' do
create_table(:users) do |t|
t.string :auth_token
t.index :auth_token, unique: true
end
valid_model = define_model_class(:User) { has_secure_token(:auth_token) }
expect(valid_model.new).to have_secure_token(:auth_token)
end
it 'does not match when missing an token index' do
create_table(:users) do |t|
t.string :token
end
invalid_model = define_model_class(:User) { has_secure_token }
expected_message =
'Expected User to have :token as a secure token but the following ' \
'errors were found: missing unique index for users.token'
aggregate_failures do
expect(invalid_model.new).not_to have_secure_token
expect { expect(invalid_model.new).to have_secure_token }.
to fail_with_message(expected_message)
end
end
it 'matches when called with ignoring_check_for_db_index without db index' do
create_table(:users) do |t|
t.string :token
end
valid_model = define_model_class(:User) { has_secure_token }
expect(valid_model.new).
to have_secure_token.ignoring_check_for_db_index
end
it 'does not match when missing a token column' do
create_table(:users)
invalid_model = define_model_class(:User) { has_secure_token }
expected_message =
'Expected User to have :token as a secure token but the following ' \
'errors were found: missing expected class and instance methods, ' \
'missing correct column token:string, missing unique index for ' \
'users.token'
aggregate_failures do
expect(invalid_model.new).not_to have_secure_token
expect { expect(invalid_model.new).to have_secure_token }.
to fail_with_message(expected_message)
end
end
it 'does not match when when lacking has_secure_token' do
create_table(:users) do |t|
t.string :token
t.index :token
end
invalid_model = define_model_class(:User)
expected_message =
'Expected User to have :token as a secure token but the following ' \
'errors were found: missing expected class and instance methods, ' \
'missing unique index for users.token'
aggregate_failures do
expect(invalid_model.new).not_to have_secure_token
expect { expect(invalid_model.new).to have_secure_token }.
to fail_with_message(expected_message)
end
end
it 'does not match when missing an index for a custom attribute' do
create_table(:users) do |t|
t.string :auth_token
end
invalid_model = define_model_class(:User) do
has_secure_token(:auth_token)
end
expected_message =
'Expected User to have :auth_token as a secure token but the ' \
'following errors were found: missing unique index for ' \
'users.auth_token'
aggregate_failures do
expect(invalid_model.new).not_to have_secure_token(:auth_token)
expect { expect(invalid_model.new).to have_secure_token(:auth_token) }.
to fail_with_message(expected_message)
end
end
it 'does not match when missing a column for a custom attribute' do
create_table(:users)
invalid_model = define_model_class(:User) do
has_secure_token(:auth_token)
end
expected_message =
'Expected User to have :auth_token as a secure token but the ' \
'following errors were found: missing expected class and instance ' \
'methods, missing correct column auth_token:string, missing unique ' \
'index for users.auth_token'
aggregate_failures do
expect(invalid_model.new).not_to have_secure_token(:auth_token)
expect { expect(invalid_model.new).to have_secure_token(:auth_token) }.
to fail_with_message(expected_message)
end
end
it 'does not match when when lacking has_secure_token for the attribute' do
create_table(:users) do |t|
t.string :auth_token
t.index :auth_token, unique: true
end
invalid_model = define_model_class(:User)
expected_message =
'Expected User to have :auth_token as a secure token but the ' \
'following errors were found: missing expected class and instance ' \
'methods'
aggregate_failures do
expect(invalid_model.new).not_to have_secure_token(:auth_token)
expect { expect(invalid_model.new).to have_secure_token(:auth_token) }.
to fail_with_message(expected_message)
end
end
it 'fails with the appropriate message when negated' do
create_table(:users) do |t|
t.string :token
t.index :token, unique: true
end
valid_model = define_model_class(:User) { has_secure_token }
expect { expect(valid_model.new).not_to have_secure_token }.
to fail_with_message('Did not expect User to have secure token :token')
end
end
end