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
4301d8cb
Unverified
Commit
4301d8cb
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/domain_allows` (#25333)
parent
841c220c
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
spec/fabricators/domain_allow_fabricator.rb
+1
-1
1 addition, 1 deletion
spec/fabricators/domain_allow_fabricator.rb
spec/requests/api/v1/admin/domain_allows_spec.rb
+214
-0
214 additions, 0 deletions
spec/requests/api/v1/admin/domain_allows_spec.rb
with
215 additions
and
1 deletion
spec/fabricators/domain_allow_fabricator.rb
+
1
−
1
View file @
4301d8cb
# frozen_string_literal: true
Fabricator
(
:domain_allow
)
do
domain
'MyString'
domain
{
sequence
(
:domain
)
{
|
i
|
"example
#{
i
}
.com"
}
}
end
This diff is collapsed.
Click to expand it.
spec/
controller
s/api/v1/admin/domain_allows_
controller_
spec.rb
→
spec/
request
s/api/v1/admin/domain_allows_spec.rb
+
214
−
0
View file @
4301d8cb
...
...
@@ -2,22 +2,19 @@
require
'rails_helper'
RSpec
.
describe
Api
::
V1
::
Admin
::
DomainAllowsController
do
render_views
let
(
:role
)
{
UserRole
.
find_by
(
name:
'Admin'
)
}
let
(
:user
)
{
Fabricate
(
:user
,
role:
role
)
}
let
(
:scopes
)
{
'admin:read admin:write'
}
let
(
:token
)
{
Fabricate
(
:accessible_access_token
,
resource_owner_id:
user
.
id
,
scopes:
scopes
)
}
before
do
allow
(
controller
).
to
receive
(
:doorkeeper_token
)
{
token
}
end
RSpec
.
describe
'Domain Allows'
do
let
(
:role
)
{
UserRole
.
find_by
(
name:
'Admin'
)
}
let
(
:user
)
{
Fabricate
(
:user
,
role:
role
)
}
let
(
:scopes
)
{
'admin:read admin:write'
}
let
(
:token
)
{
Fabricate
(
:accessible_access_token
,
resource_owner_id:
user
.
id
,
scopes:
scopes
)
}
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,115 +23,192 @@ RSpec.describe Api::V1::Admin::DomainAllowsController 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
let!
(
:domain_allow
)
{
Fabricate
(
:domain_allow
)
}
before
do
get
:index
describe
'GET /api/v1/admin/domain_allows'
do
subject
do
get
'/api/v1/admin/domain_allows'
,
headers:
headers
,
params:
params
end
let
(
:params
)
{
{}
}
it_behaves_like
'forbidden for wrong scope'
,
'write:statuses'
it_behaves_like
'forbidden for wrong role'
,
''
it_behaves_like
'forbidden for wrong role'
,
'Moderator'
it
'returns http success'
do
subject
expect
(
response
).
to
have_http_status
(
200
)
end
it
'returns the expected domain allows'
do
json
=
body_as_json
expect
(
json
.
length
).
to
eq
1
expect
(
json
[
0
][
:id
].
to_i
).
to
eq
domain_allow
.
id
context
'when there is no allowed domains'
do
it
'returns an empty body'
do
subject
expect
(
body_as_json
).
to
be_empty
end
end
end
describe
'GET #show'
do
let!
(
:domain_allow
)
{
Fabricate
(
:domain_allow
)
}
context
'when there are allowed domains'
do
let!
(
:domain_allows
)
{
Fabricate
.
times
(
5
,
:domain_allow
)
}
let
(
:expected_response
)
do
domain_allows
.
map
do
|
domain_allow
|
{
id:
domain_allow
.
id
.
to_s
,
domain:
domain_allow
.
domain
,
created_at:
domain_allow
.
created_at
.
strftime
(
'%Y-%m-%dT%H:%M:%S.%LZ'
),
}
end
end
before
do
get
:show
,
params:
{
id:
domain_allow
.
id
}
end
it
'returns the correct allowed domains'
do
subject
it_behaves_like
'forbidden for wrong scope'
,
'write:statuses'
it_behaves_like
'forbidden for wrong role'
,
''
it_behaves_like
'forbidden for wrong role'
,
'Moderator'
expect
(
body_as_json
).
to
match_array
(
expected_response
)
end
it
'returns http success'
do
expect
(
response
).
to
have_http_status
(
200
)
end
context
'with limit param'
do
let
(
:params
)
{
{
limit:
2
}
}
it
'returns only the requested number of allowed domains'
do
subject
it
'returns expected domain name'
do
json
=
body_as_json
e
xpect
(
json
[
:domain
]).
to
eq
domain_allow
.
domai
n
expect
(
body_as_json
.
size
).
to
eq
(
params
[
:limit
])
end
en
d
end
end
describe
'DELETE #destroy'
do
let!
(
:domain_allow
)
{
Fabricate
(
:domain_allow
)
}
before
do
delete
:destroy
,
params:
{
id:
domain_allow
.
id
}
describe
'GET /api/v1/admin/domain_allows/:id'
do
subject
do
get
"/api/v1/admin/domain_allows/
#{
domain_allow
.
id
}
"
,
headers:
headers
end
let!
(
:domain_allow
)
{
Fabricate
(
:domain_allow
)
}
it_behaves_like
'forbidden for wrong scope'
,
'write:statuses'
it_behaves_like
'forbidden for wrong role'
,
''
it_behaves_like
'forbidden for wrong role'
,
'Moderator'
it
'returns http success'
do
subject
expect
(
response
).
to
have_http_status
(
200
)
end
it
'deletes the block'
do
expect
(
DomainAllow
.
find_by
(
id:
domain_allow
.
id
)).
to
be_nil
it
'returns the expected allowed domain name'
do
subject
expect
(
body_as_json
[
:domain
]).
to
eq
domain_allow
.
domain
end
end
describe
'POST #create'
do
let!
(
:domain_allow
)
{
Fabricate
(
:domain_allow
,
domain:
'example.com'
)
}
context
'when the requested allowed domain does not exist'
do
it
'returns http not found'
do
get
'/api/v1/admin/domain_allows/-1'
,
headers:
headers
context
'with a valid domain'
do
before
do
post
:create
,
params:
{
domain:
'foo.bar.com'
}
expect
(
response
).
to
have_http_status
(
404
)
end
end
end
it_behaves_like
'forbidden for wrong scope'
,
'write:statuses'
it_behaves_like
'forbidden for wrong role'
,
''
it_behaves_like
'forbidden for wrong role'
,
'Moderator'
describe
'POST /api/v1/admin/domain_allows'
do
subject
do
post
'/api/v1/admin/domain_allows'
,
headers:
headers
,
params:
params
end
let
(
:params
)
{
{
domain:
'foo.bar.com'
}
}
it_behaves_like
'forbidden for wrong scope'
,
'write:statuses'
it_behaves_like
'forbidden for wrong role'
,
''
it_behaves_like
'forbidden for wrong role'
,
'Moderator'
context
'with a valid domain name'
do
it
'returns http success'
do
subject
expect
(
response
).
to
have_http_status
(
200
)
end
it
'returns expected domain name'
do
json
=
body_as_json
expect
(
json
[
:domain
]).
to
eq
'foo.bar.com'
it
'returns the expected domain name'
do
subject
expect
(
body_as_json
[
:domain
]).
to
eq
'foo.bar.com'
end
it
'creates a domain block'
do
expect
(
DomainAllow
.
find_by
(
domain:
'foo.bar.com'
)).
to_not
be_nil
it
'creates a domain allow'
do
subject
expect
(
DomainAllow
.
find_by
(
domain:
'foo.bar.com'
)).
to
be_present
end
end
context
'with invalid domain name'
do
before
do
post
:create
,
params:
{
domain:
'foo bar'
}
end
let
(
:params
)
{
'foo bar'
}
it
'returns http unprocessable entity'
do
subject
expect
(
response
).
to
have_http_status
(
422
)
end
end
context
'when domain name is not specified'
do
let
(
:params
)
{
{}
}
it
'returns http unprocessable entity'
do
post
:create
subject
expect
(
response
).
to
have_http_status
(
422
)
end
end
context
'when the domain is already allowed'
do
before
do
DomainAllow
.
create
(
params
)
end
it
'returns the existing allowed domain name'
do
subject
expect
(
body_as_json
[
:domain
]).
to
eq
(
params
[
:domain
])
end
end
end
describe
'DELETE /api/v1/admin/domain_allows/:id'
do
subject
do
delete
"/api/v1/admin/domain_allows/
#{
domain_allow
.
id
}
"
,
headers:
headers
end
let!
(
:domain_allow
)
{
Fabricate
(
:domain_allow
)
}
it_behaves_like
'forbidden for wrong scope'
,
'write:statuses'
it_behaves_like
'forbidden for wrong role'
,
''
it_behaves_like
'forbidden for wrong role'
,
'Moderator'
it
'returns http success'
do
subject
expect
(
response
).
to
have_http_status
(
200
)
end
it
'deletes the allowed domain'
do
subject
expect
(
DomainAllow
.
find_by
(
id:
domain_allow
.
id
)).
to
be_nil
end
context
'when the allowed domain does not exist'
do
it
'returns http not found'
do
delete
'/api/v1/admin/domain_allows/-1'
,
headers:
headers
expect
(
response
).
to
have_http_status
(
404
)
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