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
04166c4a
Commit
04166c4a
authored
7 years ago
by
Matt Jankowski
Committed by
Eugen Rochko
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Specs for API push controller, with refactor (#2926)
* Coverage for api push controller * Refactor the api/push controller
parent
fed585e3
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/push_controller.rb
+49
-19
49 additions, 19 deletions
app/controllers/api/push_controller.rb
spec/controllers/api/push_controller_spec.rb
+47
-2
47 additions, 2 deletions
spec/controllers/api/push_controller_spec.rb
with
96 additions
and
21 deletions
app/controllers/api/push_controller.rb
+
49
−
19
View file @
04166c4a
...
...
@@ -2,36 +2,66 @@
class
Api::PushController
<
ApiController
def
update
mode
=
params
[
'hub.mode'
]
topic
=
params
[
'hub.topic'
]
callback
=
params
[
'hub.callback'
]
lease_seconds
=
params
[
'hub.lease_seconds'
]
secret
=
params
[
'hub.secret'
]
response
,
status
=
process_push_request
render
plain:
response
,
status:
status
end
private
case
mode
def
process_push_request
case
hub_mode
when
'subscribe'
response
,
status
=
Pubsubhubbub
::
SubscribeService
.
new
.
call
(
topic_to_
account
(
topic
)
,
callback
,
secret
,
lease_seconds
)
Pubsubhubbub
::
SubscribeService
.
new
.
call
(
account
_from_
topic
,
hub_
callback
,
hub_
secret
,
hub_
lease_seconds
)
when
'unsubscribe'
response
,
status
=
Pubsubhubbub
::
UnsubscribeService
.
new
.
call
(
topic_to_
account
(
topic
)
,
callback
)
Pubsubhubbub
::
UnsubscribeService
.
new
.
call
(
account
_from_
topic
,
hub_
callback
)
else
response
=
"Unknown mode:
#{
mode
}
"
status
=
422
[
"Unknown mode:
#{
hub_mode
}
"
,
422
]
end
end
render
plain:
response
,
status:
status
def
hub_mode
params
[
'hub.mode'
]
end
private
def
hub_topic
params
[
'hub.topic'
]
end
def
hub_callback
params
[
'hub.callback'
]
end
def
hub_lease_seconds
params
[
'hub.lease_seconds'
]
end
def
hub_secret
params
[
'hub.secret'
]
end
def
topic_to_account
(
topic_url
)
return
if
topic_url
.
blank?
def
account_from_topic
if
hub_topic
.
present?
&&
local_domain?
&&
account_feed_path?
Account
.
find_local
(
hub_topic_params
[
:username
])
end
end
uri
=
Addressable
::
URI
.
parse
(
topic_url
).
normalize
params
=
Rails
.
application
.
routes
.
recognize_path
(
uri
.
path
)
domain
=
uri
.
host
+
(
uri
.
port
?
":
#{
uri
.
port
}
"
:
''
)
def
hub_topic_params
@_hub_topic_
params
||
=
Rails
.
application
.
routes
.
recognize_path
(
hub_topic_
uri
.
path
)
end
return
unless
TagManager
.
instance
.
web_domain?
(
domain
)
&&
params
[
:controller
]
==
'accounts'
&&
params
[
:action
]
==
'show'
&&
params
[
:format
]
==
'atom'
def
hub_topic_uri
@_hub_topic_uri
||=
Addressable
::
URI
.
parse
(
hub_topic
).
normalize
end
def
local_domain?
TagManager
.
instance
.
web_domain?
(
hub_topic_domain
)
end
def
hub_topic_domain
hub_topic_uri
.
host
+
(
hub_topic_uri
.
port
?
":
#{
hub_topic_uri
.
port
}
"
:
''
)
end
Account
.
find_local
(
params
[
:username
])
def
account_feed_path?
hub_topic_params
[
:controller
]
==
'accounts'
&&
hub_topic_params
[
:action
]
==
'show'
&&
hub_topic_params
[
:format
]
==
'atom'
end
end
This diff is collapsed.
Click to expand it.
spec/controllers/api/push_controller_spec.rb
+
47
−
2
View file @
04166c4a
...
...
@@ -3,11 +3,56 @@ require 'rails_helper'
RSpec
.
describe
Api
::
PushController
,
type: :controller
do
describe
'POST #update'
do
context
'with hub.mode=subscribe'
do
pending
it
'creates a subscription'
do
service
=
double
(
call:
[
''
,
202
])
allow
(
Pubsubhubbub
::
SubscribeService
).
to
receive
(
:new
).
and_return
(
service
)
account
=
Fabricate
(
:account
)
account_topic_url
=
"https://
#{
Rails
.
configuration
.
x
.
local_domain
}
/users/
#{
account
.
username
}
.atom"
post
:update
,
params:
{
'hub.mode'
=>
'subscribe'
,
'hub.topic'
=>
account_topic_url
,
'hub.callback'
=>
'https://callback.host/api'
,
'hub.lease_seconds'
=>
'3600'
,
'hub.secret'
=>
'as1234df'
,
}
expect
(
service
).
to
have_received
(
:call
).
with
(
account
,
'https://callback.host/api'
,
'as1234df'
,
'3600'
,
)
expect
(
response
).
to
have_http_status
(
:success
)
end
end
context
'with hub.mode=unsubscribe'
do
pending
it
'unsubscribes the account'
do
service
=
double
(
call:
[
''
,
202
])
allow
(
Pubsubhubbub
::
UnsubscribeService
).
to
receive
(
:new
).
and_return
(
service
)
account
=
Fabricate
(
:account
)
account_topic_url
=
"https://
#{
Rails
.
configuration
.
x
.
local_domain
}
/users/
#{
account
.
username
}
.atom"
post
:update
,
params:
{
'hub.mode'
=>
'unsubscribe'
,
'hub.topic'
=>
account_topic_url
,
'hub.callback'
=>
'https://callback.host/api'
,
}
expect
(
service
).
to
have_received
(
:call
).
with
(
account
,
'https://callback.host/api'
,
)
expect
(
response
).
to
have_http_status
(
:success
)
end
end
context
'with unknown mode'
do
it
'returns an unknown mode error'
do
post
:update
,
params:
{
'hub.mode'
=>
'fake'
}
expect
(
response
).
to
have_http_status
(
422
)
expect
(
response
.
body
).
to
match
(
/Unknown mode/
)
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