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
83435c49
Commit
83435c49
authored
7 years ago
by
Matt Jankowski
Committed by
Eugen Rochko
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Clean up api/subscriptions controller (#3448)
parent
93de41b3
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/controllers/api/subscriptions_controller.rb
+25
-6
25 additions, 6 deletions
app/controllers/api/subscriptions_controller.rb
spec/controllers/api/subscriptions_controller_spec.rb
+20
-7
20 additions, 7 deletions
spec/controllers/api/subscriptions_controller_spec.rb
with
45 additions
and
13 deletions
app/controllers/api/subscriptions_controller.rb
+
25
−
6
View file @
83435c49
...
...
@@ -5,18 +5,15 @@ class Api::SubscriptionsController < ApiController
respond_to
:txt
def
show
if
@account
.
subscription
(
api_subscription_url
(
@account
.
id
))
.
valid?
(
params
[
'hub.topic'
])
@account
.
update
(
subscription_expires_at:
Time
.
now
.
utc
+
(
params
[
'hub.lease_seconds'
]
||
86_400
).
to_i
.
second
s
)
render
plain:
HTMLEntities
.
new
.
encode
(
params
[
'hub.
challenge
'
])
,
status:
200
if
subscription
.
valid?
(
params
[
'hub.topic'
])
@account
.
update
(
subscription_expires_at:
future_expire
s
)
render
plain:
encoded_
challenge
,
status:
200
else
head
404
end
end
def
update
body
=
request
.
body
.
read
subscription
=
@account
.
subscription
(
api_subscription_url
(
@account
.
id
))
if
subscription
.
verify
(
body
,
request
.
headers
[
'HTTP_X_HUB_SIGNATURE'
])
ProcessingWorker
.
perform_async
(
@account
.
id
,
body
.
force_encoding
(
'UTF-8'
))
end
...
...
@@ -26,6 +23,28 @@ class Api::SubscriptionsController < ApiController
private
def
subscription
@_subscription
||=
@account
.
subscription
(
api_subscription_url
(
@account
.
id
)
)
end
def
body
@_body
||=
request
.
body
.
read
end
def
encoded_challenge
HTMLEntities
.
new
.
encode
(
params
[
'hub.challenge'
])
end
def
future_expires
Time
.
now
.
utc
+
lease_seconds_or_default
end
def
lease_seconds_or_default
(
params
[
'hub.lease_seconds'
]
||
86_400
).
to_i
.
seconds
end
def
set_account
@account
=
Account
.
find
(
params
[
:id
])
end
...
...
This diff is collapsed.
Click to expand it.
spec/controllers/api/subscriptions_controller_spec.rb
+
20
−
7
View file @
83435c49
...
...
@@ -6,16 +6,29 @@ RSpec.describe Api::SubscriptionsController, type: :controller do
let
(
:account
)
{
Fabricate
(
:account
,
username:
'gargron'
,
domain:
'quitter.no'
,
remote_url:
'topic_url'
,
secret:
'abc'
)
}
describe
'GET #show'
do
before
do
get
:show
,
params:
{
:id
=>
account
.
id
,
'hub.topic'
=>
'topic_url'
,
'hub.challenge'
=>
'456'
,
'hub.lease_seconds'
=>
"
#{
86400
*
30
}
"
}
end
context
'with valid subscription'
do
before
do
get
:show
,
params:
{
:id
=>
account
.
id
,
'hub.topic'
=>
'topic_url'
,
'hub.challenge'
=>
'456'
,
'hub.lease_seconds'
=>
"
#{
86400
*
30
}
"
}
end
it
'returns http success'
do
expect
(
response
).
to
have_http_status
(
:success
)
it
'returns http success'
do
expect
(
response
).
to
have_http_status
(
:success
)
end
it
'echoes back the challenge'
do
expect
(
response
.
body
).
to
match
'456'
end
end
it
'echoes back the challenge'
do
expect
(
response
.
body
).
to
match
'456'
context
'with invalid subscription'
do
before
do
expect_any_instance_of
(
Account
).
to
receive_message_chain
(
:subscription
,
:valid?
).
and_return
(
false
)
get
:show
,
params:
{
:id
=>
account
.
id
}
end
it
'returns http success'
do
expect
(
response
).
to
have_http_status
(
:missing
)
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