Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mastodon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
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
Show more breadcrumbs
Pierre Boudes
mastodon
Commits
99216e34
Unverified
Commit
99216e34
authored
1 year ago
by
Daniel M Brasil
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Migrate to request specs in `/api/v1/admin/canonical_email_blocks` (#25330)
parent
b4e19f96
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
spec/requests/api/v1/admin/canonical_email_blocks_spec.rb
+305
-0
305 additions, 0 deletions
spec/requests/api/v1/admin/canonical_email_blocks_spec.rb
with
305 additions
and
0 deletions
spec/
controller
s/api/v1/admin/canonical_email_blocks_
controller_
spec.rb
→
spec/
request
s/api/v1/admin/canonical_email_blocks_spec.rb
+
305
−
0
View file @
99216e34
...
...
@@ -2,22 +2,19 @@
require
'rails_helper'
describe
Api
::
V1
::
Admin
::
CanonicalEmailBlocksController
do
render_views
RSpec
.
describe
'Canonical Email Blocks'
do
let
(
:role
)
{
UserRole
.
find_by
(
name:
'Admin'
)
}
let
(
:user
)
{
Fabricate
(
:user
,
role:
role
)
}
let
(
:token
)
{
Fabricate
(
:accessible_access_token
,
resource_owner_id:
user
.
id
,
scopes:
scopes
)
}
let
(
:scopes
)
{
'admin:read:canonical_email_blocks admin:write:canonical_email_blocks'
}
before
do
allow
(
controller
).
to
receive
(
:doorkeeper_token
)
{
token
}
end
let
(
:headers
)
{
{
'Authorization'
=>
"Bearer
#{
token
.
token
}
"
}
}
shared_examples
'forbidden for wrong scope'
do
|
wrong_scope
|
let
(
:scopes
)
{
wrong_scope
}
it
'returns http forbidden'
do
subject
expect
(
response
).
to
have_http_status
(
403
)
end
end
...
...
@@ -26,65 +23,54 @@ describe Api::V1::Admin::CanonicalEmailBlocksController do
let
(
:role
)
{
UserRole
.
find_by
(
name:
wrong_role
)
}
it
'returns http forbidden'
do
subject
expect
(
response
).
to
have_http_status
(
403
)
end
end
describe
'GET #index'
do
context
'with wrong scope'
do
before
do
get
:index
end
it_behaves_like
'forbidden for wrong scope'
,
'read:statuses'
describe
'GET /api/v1/admin/canonical_email_blocks'
do
subject
do
get
'/api/v1/admin/canonical_email_blocks'
,
headers:
headers
,
params:
params
end
context
'with wrong role'
do
before
do
get
:index
end
let
(
:params
)
{
{}
}
it_behaves_like
'forbidden for wrong
rol
e'
,
''
it_behaves_like
'forbidden for wrong role'
,
'
Moderator
'
end
it_behaves_like
'forbidden for wrong
scop
e'
,
'
read:statuses
'
it_behaves_like
'forbidden for wrong role'
,
''
it_behaves_like
'forbidden for wrong role'
,
'Moderator'
it
'returns http success'
do
get
:index
subject
expect
(
response
).
to
have_http_status
(
200
)
end
context
'when there is no canonical email block'
do
it
'returns an empty list'
do
get
:index
body
=
body_as_json
subject
expect
(
body
).
to
be_empty
expect
(
body
_as_json
).
to
be_empty
end
end
context
'when there are canonical email blocks'
do
let!
(
:canonical_email_blocks
)
{
Fabricate
.
times
(
5
,
:canonical_email_block
)
}
let
(
:expected_email_hashes
)
{
canonical_email_blocks
.
pluck
(
:canonical_email_hash
)
}
let
(
:expected_email_hashes
)
{
canonical_email_blocks
.
pluck
(
:canonical_email_hash
)
}
it
'returns the correct canonical email hashes'
do
get
:index
subject
json
=
body_as_json
expect
(
json
.
pluck
(
:canonical_email_hash
)).
to
match_array
(
expected_email_hashes
)
expect
(
body_as_json
.
pluck
(
:canonical_email_hash
)).
to
match_array
(
expected_email_hashes
)
end
context
'with limit param'
do
let
(
:params
)
{
{
limit:
2
}
}
it
'returns only the requested number of canonical email blocks'
do
get
:index
,
params:
params
subject
json
=
body_as_json
expect
(
json
.
size
).
to
eq
(
params
[
:limit
])
expect
(
body_as_json
.
size
).
to
eq
(
params
[
:limit
])
end
end
...
...
@@ -92,12 +78,11 @@ describe Api::V1::Admin::CanonicalEmailBlocksController do
let
(
:params
)
{
{
since_id:
canonical_email_blocks
[
1
].
id
}
}
it
'returns only the canonical email blocks after since_id'
do
get
:index
,
params:
params
subject
canonical_email_blocks_ids
=
canonical_email_blocks
.
pluck
(
:id
).
map
(
&
:to_s
)
json
=
body_as_json
expect
(
json
.
pluck
(
:id
)).
to
match_array
(
canonical_email_blocks_ids
[
2
..
])
expect
(
body_as_
json
.
pluck
(
:id
)).
to
match_array
(
canonical_email_blocks_ids
[
2
..
])
end
end
...
...
@@ -105,47 +90,36 @@ describe Api::V1::Admin::CanonicalEmailBlocksController do
let
(
:params
)
{
{
max_id:
canonical_email_blocks
[
3
].
id
}
}
it
'returns only the canonical email blocks before max_id'
do
get
:index
,
params:
params
subject
canonical_email_blocks_ids
=
canonical_email_blocks
.
pluck
(
:id
).
map
(
&
:to_s
)
json
=
body_as_json
expect
(
json
.
pluck
(
:id
)).
to
match_array
(
canonical_email_blocks_ids
[
..
2
])
expect
(
body_as_
json
.
pluck
(
:id
)).
to
match_array
(
canonical_email_blocks_ids
[
..
2
])
end
end
end
end
describe
'GET #show'
do
let!
(
:canonical_email_block
)
{
Fabricate
(
:canonical_email_block
)
}
let
(
:params
)
{
{
id:
canonical_email_block
.
id
}
}
context
'with wrong scope'
do
before
do
get
:show
,
params:
params
end
it_behaves_like
'forbidden for wrong scope'
,
'read:statuses'
describe
'GET /api/v1/admin/canonical_email_blocks/:id'
do
subject
do
get
"/api/v1/admin/canonical_email_blocks/
#{
canonical_email_block
.
id
}
"
,
headers:
headers
end
context
'with wrong role'
do
before
do
get
:show
,
params:
params
end
let!
(
:canonical_email_block
)
{
Fabricate
(
:canonical_email_block
)
}
it_behaves_like
'forbidden for wrong
rol
e'
,
''
it_behaves_like
'forbidden for wrong role'
,
'
Moderator
'
end
it_behaves_like
'forbidden for wrong
scop
e'
,
'
read:statuses
'
it_behaves_like
'forbidden for wrong role'
,
''
it_behaves_like
'forbidden for wrong role'
,
'Moderator'
context
'when canonical email block exists'
do
context
'when
the requested
canonical email block exists'
do
it
'returns http success'
do
get
:show
,
params:
params
subject
expect
(
response
).
to
have_http_status
(
200
)
end
it
'returns canonical email block data correctly'
do
get
:show
,
params:
params
it
'returns
the requested
canonical email block data correctly'
do
subject
json
=
body_as_json
...
...
@@ -154,138 +128,116 @@ describe Api::V1::Admin::CanonicalEmailBlocksController do
end
end
context
'when canonical block does not exist'
do
context
'when
the requested
canonical block does not exist'
do
it
'returns http not found'
do
get
:show
,
params:
{
id:
0
}
get
'/api/v1/admin/canonical_email_blocks/-1'
,
headers:
headers
expect
(
response
).
to
have_http_status
(
404
)
end
end
end
describe
'POST #test'
do
context
'with wrong scope'
do
before
do
post
:test
end
it_behaves_like
'forbidden for wrong scope'
,
'read:statuses'
describe
'POST /api/v1/admin/canonical_email_blocks/test'
do
subject
do
post
'/api/v1/admin/canonical_email_blocks/test'
,
headers:
headers
,
params:
params
end
context
'with wrong role'
do
before
do
post
:test
,
params:
{
email:
'whatever@email.com'
}
end
let
(
:params
)
{
{
email:
'email@example.com'
}
}
it_behaves_like
'forbidden for wrong role'
,
''
it_behaves_like
'forbidden for wrong role'
,
'Moderator'
end
it_behaves_like
'forbidden for wrong scope'
,
'read:statuses'
it_behaves_like
'forbidden for wrong role'
,
''
it_behaves_like
'forbidden for wrong role'
,
'Moderator'
context
'when the required email param is not provided'
do
let
(
:params
)
{
{}
}
context
'when required email is not provided'
do
it
'returns http bad request'
do
post
:tes
t
subjec
t
expect
(
response
).
to
have_http_status
(
400
)
end
end
context
'when required email is provided'
do
let
(
:params
)
{
{
email:
'example@email.com'
}
}
context
'when the required email param is provided'
do
context
'when there is a matching canonical email block'
do
let!
(
:canonical_email_block
)
{
CanonicalEmailBlock
.
create
(
params
)
}
it
'returns http success'
do
post
:test
,
params:
params
subject
expect
(
response
).
to
have_http_status
(
200
)
end
it
'returns expected canonical email hash'
do
post
:test
,
params:
params
json
=
body_as_json
it
'returns the expected canonical email hash'
do
subject
expect
(
json
[
0
][
:canonical_email_hash
]).
to
eq
(
canonical_email_block
.
canonical_email_hash
)
expect
(
body_as_
json
[
0
][
:canonical_email_hash
]).
to
eq
(
canonical_email_block
.
canonical_email_hash
)
end
end
context
'when there is no matching canonical email block'
do
it
'returns http success'
do
post
:test
,
params:
params
subject
expect
(
response
).
to
have_http_status
(
200
)
end
it
'returns an empty list'
do
post
:test
,
params:
params
subject
json
=
body_as_json
expect
(
json
).
to
be_empty
expect
(
body_as_json
).
to
be_empty
end
end
end
end
describe
'POST #create'
do
let
(
:params
)
{
{
email:
'example@email.com'
}
}
let
(
:canonical_email_block
)
{
CanonicalEmailBlock
.
new
(
email:
params
[
:email
])
}
context
'with wrong scope'
do
before
do
post
:create
,
params:
params
end
it_behaves_like
'forbidden for wrong scope'
,
'read:statuses'
describe
'POST /api/v1/admin/canonical_email_blocks'
do
subject
do
post
'/api/v1/admin/canonical_email_blocks'
,
headers:
headers
,
params:
params
end
context
'with wrong role'
do
before
do
post
:create
,
params:
params
end
let
(
:params
)
{
{
email:
'example@email.com'
}
}
let
(
:canonical_email_block
)
{
CanonicalEmailBlock
.
new
(
email:
params
[
:email
])
}
it_behaves_like
'forbidden for wrong
rol
e'
,
''
it_behaves_like
'forbidden for wrong role'
,
'
Moderator
'
end
it_behaves_like
'forbidden for wrong
scop
e'
,
'
read:statuses
'
it_behaves_like
'forbidden for wrong role'
,
''
it_behaves_like
'forbidden for wrong role'
,
'Moderator'
it
'returns http success'
do
post
:create
,
params:
params
subject
expect
(
response
).
to
have_http_status
(
200
)
end
it
'returns canonical_email_hash correctly'
do
post
:create
,
params:
params
it
'returns
the
canonical_email_hash correctly'
do
subject
json
=
body_as_json
expect
(
json
[
:canonical_email_hash
]).
to
eq
(
canonical_email_block
.
canonical_email_hash
)
expect
(
body_as_json
[
:canonical_email_hash
]).
to
eq
(
canonical_email_block
.
canonical_email_hash
)
end
context
'when required email param is not provided'
do
context
'when the required email param is not provided'
do
let
(
:params
)
{
{}
}
it
'returns http unprocessable entity'
do
post
:create
subject
expect
(
response
).
to
have_http_status
(
422
)
end
end
context
'when canonical_email_hash param is provided instead of email'
do
context
'when
the
canonical_email_hash param is provided instead of email'
do
let
(
:params
)
{
{
canonical_email_hash:
'dd501ce4e6b08698f19df96f2f15737e48a75660b1fa79b6ff58ea25ee4851a4'
}
}
it
'returns http success'
do
post
:create
,
params:
params
subject
expect
(
response
).
to
have_http_status
(
200
)
end
it
'returns correct canonical_email_hash'
do
post
:create
,
params:
params
json
=
body_as_json
it
'returns the correct canonical_email_hash'
do
subject
expect
(
json
[
:canonical_email_hash
]).
to
eq
(
params
[
:canonical_email_hash
])
expect
(
body_as_
json
[
:canonical_email_hash
]).
to
eq
(
params
[
:canonical_email_hash
])
end
end
...
...
@@ -293,63 +245,58 @@ describe Api::V1::Admin::CanonicalEmailBlocksController do
let
(
:params
)
{
{
email:
'example@email.com'
,
canonical_email_hash:
'dd501ce4e6b08698f19df96f2f15737e48a75660b1fa79b6ff58ea25ee4851a4'
}
}
it
'returns http success'
do
post
:create
,
params:
params
subject
expect
(
response
).
to
have_http_status
(
200
)
end
it
'ignores canonical_email_hash param'
do
post
:create
,
params:
params
json
=
body_as_json
it
'ignores the canonical_email_hash param'
do
subject
expect
(
json
[
:canonical_email_hash
]).
to
eq
(
canonical_email_block
.
canonical_email_hash
)
expect
(
body_as_
json
[
:canonical_email_hash
]).
to
eq
(
canonical_email_block
.
canonical_email_hash
)
end
end
context
'when canonical email was already blocked'
do
context
'when
the given
canonical email was already blocked'
do
before
do
canonical_email_block
.
save
end
it
'returns http unprocessable entity'
do
post
:create
,
params:
params
subject
expect
(
response
).
to
have_http_status
(
422
)
end
end
end
describe
'DELETE #destroy'
do
describe
'DELETE /api/v1/admin/canonical_email_blocks/:id'
do
subject
do
delete
"/api/v1/admin/canonical_email_blocks/
#{
canonical_email_block
.
id
}
"
,
headers:
headers
end
let!
(
:canonical_email_block
)
{
Fabricate
(
:canonical_email_block
)
}
let
(
:params
)
{
{
id:
canonical_email_block
.
id
}
}
context
'with wrong scope'
do
before
do
delete
:destroy
,
params:
params
end
it_behaves_like
'forbidden for wrong scope'
,
'read:statuses'
it_behaves_like
'forbidden for wrong
scop
e'
,
'
read:statuses
'
end
it_behaves_like
'forbidden for wrong
rol
e'
,
''
it_behaves_like
'forbidden for wrong role'
,
'Moderator'
context
'with wrong role'
do
before
do
delete
:destroy
,
params:
params
end
it
'returns http success'
do
subject
it_behaves_like
'forbidden for wrong role'
,
''
it_behaves_like
'forbidden for wrong role'
,
'Moderator'
expect
(
response
).
to
have_http_status
(
200
)
end
it
'
returns http success
'
do
delete
:destroy
,
params:
params
it
'
deletes the canonical email block
'
do
subject
expect
(
response
).
to
have_http_status
(
200
)
expect
(
CanonicalEmailBlock
.
find_by
(
id:
canonical_email_block
.
id
)).
to
be_nil
end
context
'when canonical email block is not found'
do
context
'when
the
canonical email block is not found'
do
it
'returns http not found'
do
delete
:destroy
,
params:
{
id:
0
}
delete
'/api/v1/admin/canonical_email_blocks/0'
,
headers:
headers
expect
(
response
).
to
have_http_status
(
404
)
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