Skip to content

Commit

Permalink
Change GET /invite/:invite_code to return a JSON document when JSON…
Browse files Browse the repository at this point in the history
… is requested
  • Loading branch information
ClearlyClaire committed Nov 10, 2023
1 parent 81f4bfc commit 5ecb3c6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/controllers/api/v1/invites_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

class Api::V1::InvitesController < Api::BaseController
include RegistrationHelper
include RoutingHelper

skip_before_action :require_authenticated_user!
skip_around_action :set_locale

before_action :set_invite
before_action :check_enabled_registrations!

# Override `current_user` to avoid reading session cookies
def current_user; end

def show
render json: { invite_code: params[:invite_code], instance_api_url: api_v2_instance_url }, status: 200
end

private

def set_invite
@invite = Invite.find_by!(code: params[:invite_code])
end

def check_enabled_registrations!
# TODO: more specific error message when an invite has expired
raise Mastodon::NotPermittedError unless allowed_registration?(request.remote_ip, @invite)
end
end
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ def redirect_with_vary(path)
resource :outbox, only: [:show], module: :activitypub
end

# TODO
get '/invite/:invite_code', constraints: ->(req) { req.format == :json }, to: 'api/v1/invites#show'

devise_scope :user do
get '/invite/:invite_code', to: 'auth/registrations#new', as: :public_invite

Expand Down
27 changes: 27 additions & 0 deletions spec/requests/invite_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require 'rails_helper'

describe 'invites' do
let(:invite) { Fabricate(:invite) }

context 'when requesting a JSON document' do
it 'returns a JSON document with expected attributes' do
get "/invite/#{invite.code}", headers: { 'Accept' => 'application/activity+json' }

expect(response).to have_http_status(200)
expect(response.media_type).to eq 'application/json'

expect(body_as_json[:invite_code]).to eq invite.code
end
end

context 'when not requesting a JSON document' do
it 'returns an HTML page' do
get "/invite/#{invite.code}"

expect(response).to have_http_status(200)
expect(response.media_type).to eq 'text/html'
end
end
end

0 comments on commit 5ecb3c6

Please sign in to comment.