Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mastofe
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Pleroma
mastofe
Commits
05edec69
Commit
05edec69
authored
6 years ago
by
ysksn
Committed by
Eugen Rochko
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Add specs for BlackListedEmailValidator (#9651)
* Add specs for BlackListedEmailValidator * Use instance variable
parent
29484f65
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/validators/blacklisted_email_validator.rb
+9
-8
9 additions, 8 deletions
app/validators/blacklisted_email_validator.rb
spec/validators/blacklisted_email_validator_spec.rb
+31
-0
31 additions, 0 deletions
spec/validators/blacklisted_email_validator_spec.rb
with
40 additions
and
8 deletions
app/validators/blacklisted_email_validator.rb
+
9
−
8
View file @
05edec69
...
...
@@ -2,31 +2,32 @@
class
BlacklistedEmailValidator
<
ActiveModel
::
Validator
def
validate
(
user
)
user
.
errors
.
add
(
:email
,
I18n
.
t
(
'users.invalid_email'
))
if
blocked_email?
(
user
.
email
)
@email
=
user
.
email
user
.
errors
.
add
(
:email
,
I18n
.
t
(
'users.invalid_email'
))
if
blocked_email?
end
private
def
blocked_email?
(
value
)
on_blacklist?
(
value
)
||
not_on_whitelist?
(
value
)
def
blocked_email?
on_blacklist?
||
not_on_whitelist?
end
def
on_blacklist?
(
value
)
return
true
if
EmailDomainBlock
.
block?
(
value
)
def
on_blacklist?
return
true
if
EmailDomainBlock
.
block?
(
@email
)
return
false
if
Rails
.
configuration
.
x
.
email_domains_blacklist
.
blank?
domains
=
Rails
.
configuration
.
x
.
email_domains_blacklist
.
gsub
(
'.'
,
'\.'
)
regexp
=
Regexp
.
new
(
"@(.+
\\
.)?(
#{
domains
}
)"
,
true
)
value
=~
regexp
@email
=~
regexp
end
def
not_on_whitelist?
(
value
)
def
not_on_whitelist?
return
false
if
Rails
.
configuration
.
x
.
email_domains_whitelist
.
blank?
domains
=
Rails
.
configuration
.
x
.
email_domains_whitelist
.
gsub
(
'.'
,
'\.'
)
regexp
=
Regexp
.
new
(
"@(.+
\\
.)?(
#{
domains
}
)$"
,
true
)
value
!~
regexp
@email
!~
regexp
end
end
This diff is collapsed.
Click to expand it.
spec/validators/blacklisted_email_validator_spec.rb
0 → 100644
+
31
−
0
View file @
05edec69
# frozen_string_literal: true
require
'rails_helper'
RSpec
.
describe
BlacklistedEmailValidator
,
type: :validator
do
describe
'#validate'
do
let
(
:user
)
{
double
(
email:
'info@mail.com'
,
errors:
errors
)
}
let
(
:errors
)
{
double
(
add:
nil
)
}
before
do
allow_any_instance_of
(
described_class
).
to
receive
(
:blocked_email?
)
{
blocked_email
}
described_class
.
new
.
validate
(
user
)
end
context
'blocked_email?'
do
let
(
:blocked_email
)
{
true
}
it
'calls errors.add'
do
expect
(
errors
).
to
have_received
(
:add
).
with
(
:email
,
I18n
.
t
(
'users.invalid_email'
))
end
end
context
'!blocked_email?'
do
let
(
:blocked_email
)
{
false
}
it
'not calls errors.add'
do
expect
(
errors
).
not_to
have_received
(
:add
).
with
(
:email
,
I18n
.
t
(
'users.invalid_email'
))
end
end
end
end
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment