Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
3e4855a6
Commit
3e4855a6
authored
Jan 27, 2020
by
charlie ablett
Committed by
Sean McGivern
Jan 27, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add AkismetService unit tests
parent
b9daa5c1
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
137 additions
and
0 deletions
+137
-0
spec/services/akismet_service_spec.rb
spec/services/akismet_service_spec.rb
+137
-0
No files found.
spec/services/akismet_service_spec.rb
0 → 100644
View file @
3e4855a6
# frozen_string_literal: true
require
'spec_helper'
describe
AkismetService
do
let
(
:fake_akismet_client
)
{
double
(
:akismet_client
)
}
let_it_be
(
:text
)
{
"Would you like to buy some tinned meat product?"
}
let_it_be
(
:spam_owner
)
{
create
(
:user
)
}
subject
do
options
=
{
ip_address:
'1.2.3.4'
,
user_agent:
'some user_agent'
,
referrer:
'some referrer'
}
described_class
.
new
(
spam_owner
.
name
,
spam_owner
.
email
,
text
,
options
)
end
before
do
stub_application_setting
(
akismet_enabled:
true
)
allow
(
subject
).
to
receive
(
:akismet_client
).
and_return
(
fake_akismet_client
)
end
shared_examples
'no activity if Akismet is not enabled'
do
|
method_call
|
# if the method name is `submit`, it requires an argument, so add it
before
do
stub_application_setting
(
akismet_enabled:
false
)
end
it
'is automatically false'
do
expect
(
subject
.
send
(
method_call
)).
to
be_falsey
end
it
'performs no check'
do
expect
(
fake_akismet_client
).
not_to
receive
(
:public_send
)
subject
.
send
(
method_call
)
end
end
shared_examples
'false if Akismet is not available'
do
|
method_call
|
context
'if Akismet is not available'
do
before
do
allow
(
fake_akismet_client
).
to
receive
(
:public_send
).
and_raise
(
StandardError
.
new
(
"oh noes!"
))
end
specify
do
expect
(
subject
.
send
(
method_call
)).
to
be_falsey
end
it
'logs an error'
do
logger_spy
=
double
(
:logger
)
expect
(
Rails
).
to
receive
(
:logger
).
and_return
(
logger_spy
)
expect
(
logger_spy
).
to
receive
(
:error
).
with
(
/skipping/
)
subject
.
send
(
method_call
)
end
end
end
describe
'#spam?'
do
it_behaves_like
'no activity if Akismet is not enabled'
,
:spam?
,
:check
context
'if Akismet is enabled'
do
context
'the text is spam'
do
before
do
allow
(
fake_akismet_client
).
to
receive
(
:check
).
and_return
([
true
,
false
])
end
specify
do
expect
(
subject
.
spam?
).
to
be_truthy
end
end
context
'the text is blatant spam'
do
before
do
allow
(
fake_akismet_client
).
to
receive
(
:check
).
and_return
([
false
,
true
])
end
specify
do
expect
(
subject
.
spam?
).
to
be_truthy
end
end
context
'the text is not spam'
do
before
do
allow
(
fake_akismet_client
).
to
receive
(
:check
).
and_return
([
false
,
false
])
end
specify
do
expect
(
subject
.
spam?
).
to
be_falsey
end
end
context
'if Akismet is not available'
do
before
do
allow
(
fake_akismet_client
).
to
receive
(
:check
).
and_raise
(
StandardError
.
new
(
"oh noes!"
))
end
specify
do
expect
(
subject
.
spam?
).
to
be_falsey
end
it
'logs an error'
do
logger_spy
=
double
(
:logger
)
expect
(
Rails
).
to
receive
(
:logger
).
and_return
(
logger_spy
)
expect
(
logger_spy
).
to
receive
(
:error
).
with
(
/skipping check/
)
subject
.
spam?
end
end
end
end
describe
'#submit_ham'
do
it_behaves_like
'no activity if Akismet is not enabled'
,
:submit_ham
it_behaves_like
'false if Akismet is not available'
,
:submit_ham
context
'if Akismet is available'
do
specify
do
expect
(
fake_akismet_client
).
to
receive
(
:public_send
).
with
(
:ham
,
any_args
)
expect
(
subject
.
submit_ham
).
to
be_truthy
end
end
end
describe
'#submit_spam'
do
it_behaves_like
'no activity if Akismet is not enabled'
,
:submit_spam
it_behaves_like
'false if Akismet is not available'
,
:submit_spam
context
'if Akismet is available'
do
specify
do
expect
(
fake_akismet_client
).
to
receive
(
:public_send
).
with
(
:spam
,
any_args
)
expect
(
subject
.
submit_spam
).
to
be_truthy
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment