2021-02-16 17:24:42 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
describe CustomWizard::RealtimeValidationsController do
|
2024-10-16 13:52:03 +02:00
|
|
|
fab!(:user)
|
2022-07-27 12:47:50 +02:00
|
|
|
let(:validation_type) { "test_stub" }
|
2024-10-16 13:52:03 +02:00
|
|
|
let(:validation_type_stub) do
|
|
|
|
{ types: [:text], component: "similar-topics-validator", backend: true, required_params: [] }
|
|
|
|
end
|
2021-02-16 17:24:42 +01:00
|
|
|
|
2022-07-27 12:47:50 +02:00
|
|
|
before do
|
|
|
|
sign_in(user)
|
2021-02-16 17:24:42 +01:00
|
|
|
|
|
|
|
class CustomWizard::RealtimeValidation::TestStub
|
|
|
|
attr_accessor :user
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform(params)
|
|
|
|
result = CustomWizard::RealtimeValidation::Result.new(:test_stub)
|
2024-10-16 13:52:03 +02:00
|
|
|
result.items = %w[hello world]
|
2021-02-16 17:24:42 +01:00
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ::CustomWizard::RealtimeValidation::TestStubSerializer < ApplicationSerializer
|
|
|
|
attributes :item
|
|
|
|
|
|
|
|
def item
|
|
|
|
object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "gives the correct response for a given type" do
|
2022-07-27 12:47:50 +02:00
|
|
|
CustomWizard::RealtimeValidation.types = { test_stub: validation_type_stub }
|
2024-10-16 13:52:03 +02:00
|
|
|
get "/realtime-validations.json", params: { type: validation_type }
|
2021-02-16 17:24:42 +01:00
|
|
|
expect(response.status).to eq(200)
|
2024-10-16 13:52:03 +02:00
|
|
|
expected_response = [{ "item" => "hello" }, { "item" => "world" }]
|
2021-02-16 17:24:42 +01:00
|
|
|
expect(JSON.parse(response.body)).to eq(expected_response)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "gives 400 error when no type is passed" do
|
2022-07-27 12:47:50 +02:00
|
|
|
CustomWizard::RealtimeValidation.types = { test_stub: validation_type_stub }
|
2024-10-16 13:52:03 +02:00
|
|
|
get "/realtime-validations.json"
|
2021-02-16 19:13:16 +01:00
|
|
|
expect(response.status).to eq(400)
|
|
|
|
end
|
2021-02-16 17:24:42 +01:00
|
|
|
|
2021-02-16 19:13:16 +01:00
|
|
|
it "gives 400 error when a required additional param is missing" do
|
2022-07-27 12:47:50 +02:00
|
|
|
CustomWizard::RealtimeValidation.types = { test_stub: validation_type_stub }
|
2021-02-16 19:13:16 +01:00
|
|
|
CustomWizard::RealtimeValidation.types[:test_stub][:required_params] = [:test1]
|
2024-10-16 13:52:03 +02:00
|
|
|
get "/realtime-validations.json", params: { type: validation_type }
|
2021-02-16 17:24:42 +01:00
|
|
|
expect(response.status).to eq(400)
|
2021-02-16 19:13:16 +01:00
|
|
|
# the addition is only relevant to this test, so getting rid of it
|
|
|
|
CustomWizard::RealtimeValidation.types[:test_stub][:required_params] = []
|
2021-02-16 17:24:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "gives 500 response code when a non existant type is passed" do
|
2022-07-27 12:47:50 +02:00
|
|
|
CustomWizard::RealtimeValidation.types = { test_stub: validation_type_stub }
|
2024-10-16 13:52:03 +02:00
|
|
|
get "/realtime-validations.json", params: { type: "random_type" }
|
2021-02-16 17:24:42 +01:00
|
|
|
expect(response.status).to eq(500)
|
|
|
|
end
|
|
|
|
end
|