0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-14 05:52:52 +01:00
discourse-custom-wizard/spec/requests/custom_wizard/admin/subscription_controller_spec.rb

72 Zeilen
2,5 KiB
Ruby

2021-09-07 14:06:13 +02:00
# frozen_string_literal: true
require_relative '../../../plugin_helper'
2021-09-24 11:58:42 +02:00
describe CustomWizard::AdminSubscriptionController do
2021-09-07 14:06:13 +02:00
fab!(:admin_user) { Fabricate(:user, admin: true) }
def generate_payload(request_id, user_id)
2021-09-24 11:58:42 +02:00
uri = URI(@subscription.authentication_url(user_id, request_id))
keys = @subscription.authentication.get_keys(request_id)
2021-09-07 14:06:13 +02:00
raw_payload = {
key: "12345",
nonce: keys.nonce,
push: false,
api: UserApiKeysController::AUTH_API_VERSION
}.to_json
public_key = OpenSSL::PKey::RSA.new(keys.pem)
Base64.encode64(public_key.public_encrypt(raw_payload))
end
before do
2021-09-24 11:58:42 +02:00
@subscription = CustomWizard::Subscription.new
2021-09-07 14:06:13 +02:00
sign_in(admin_user)
end
it "#index" do
2021-09-24 11:58:42 +02:00
get "/admin/wizards/subscription.json"
expect(response.parsed_body['server']).to eq(@subscription.server)
expect(response.parsed_body['authentication'].deep_symbolize_keys).to eq(CustomWizard::Subscription::AuthenticationSerializer.new(@subscription.authentication, root: false).as_json)
expect(response.parsed_body['subscription'].deep_symbolize_keys).to eq(CustomWizard::Subscription::SubscriptionSerializer.new(@subscription.subscription, root: false).as_json)
2021-09-07 14:06:13 +02:00
end
it "#authorize" do
2021-09-24 11:58:42 +02:00
get "/admin/wizards/subscription/authorize"
2021-09-07 14:06:13 +02:00
expect(response.status).to eq(302)
expect(cookies[:user_api_request_id].present?).to eq(true)
end
it "#destroy_authentication" do
request_id = SecureRandom.hex(32)
payload = generate_payload(request_id, admin_user.id)
2021-09-24 11:58:42 +02:00
@subscription.authentication_response(request_id, payload)
2021-09-07 14:06:13 +02:00
2021-09-24 11:58:42 +02:00
delete "/admin/wizards/subscription/authorize.json"
2021-09-07 14:06:13 +02:00
expect(response.status).to eq(200)
2021-09-24 11:58:42 +02:00
expect(CustomWizard::Subscription.authorized?).to eq(false)
2021-09-07 14:06:13 +02:00
end
context "subscription" do
before do
stub_subscription_request(200, valid_subscription)
end
it "handles authentication response and the updates subscription" do
request_id = cookies[:user_api_request_id] = SecureRandom.hex(32)
payload = generate_payload(request_id, admin_user.id)
2021-09-24 11:58:42 +02:00
get "/admin/wizards/subscription/authorize/callback", params: { payload: payload }
2021-09-07 14:06:13 +02:00
2021-09-24 11:58:42 +02:00
expect(response).to redirect_to("/admin/wizards/subscription")
expect(CustomWizard::Subscription.subscribed?).to eq(true)
2021-09-07 14:06:13 +02:00
end
it "updates the subscription" do
2021-09-24 11:58:42 +02:00
authenticate_subscription
post "/admin/wizards/subscription.json"
2021-09-07 14:06:13 +02:00
expect(response.status).to eq(200)
2021-09-24 11:58:42 +02:00
expect(CustomWizard::Subscription.subscribed?).to eq(true)
2021-09-07 14:06:13 +02:00
end
end
2021-09-07 14:11:50 +02:00
end