Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions spec/values/all_casa_admin_parameters_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
require "rails_helper"

RSpec.describe AllCasaAdminParameters do
# TODO: Add tests for AllCasaAdminParameters
subject { described_class.new(params) }

pending "add some tests for AllCasaAdminParameters"
let(:params) {
ActionController::Parameters.new(
all_casa_admin: ActionController::Parameters.new(
email: "all_admin@example.com",
password: "password123"
)
)
}

it "permits the allowed attributes" do
expect(subject["email"]).to eq("all_admin@example.com")
expect(subject["password"]).to eq("password123")
end

it "filters out attributes that are not permitted" do
params[:all_casa_admin][:admin] = true
expect(subject["admin"]).to be_nil
end

it "raises when the all_casa_admin key is missing" do
expect {
described_class.new(ActionController::Parameters.new(user: {}))
}.to raise_error(ActionController::ParameterMissing)
end

describe "#with_password" do
it "sets the password and returns self" do
result = subject.with_password("new-password")

expect(result).to equal(subject)
expect(subject["password"]).to eq("new-password")
end
end
end
29 changes: 27 additions & 2 deletions spec/values/casa_admin_parameters_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
require "rails_helper"

RSpec.describe CasaAdminParameters do
# TODO: Add tests for CasaAdminParameters
subject { described_class.new(params) }

pending "add some tests for CasaAdminParameters"
let(:params) {
ActionController::Parameters.new(
casa_admin: ActionController::Parameters.new(
email: "admin@example.com",
display_name: "Admin Name"
)
)
}

it "wraps params under the casa_admin root key" do
expect(subject["email"]).to eq("admin@example.com")
expect(subject["display_name"]).to eq("Admin Name")
end

it "raises when the casa_admin key is missing" do
expect {
described_class.new(ActionController::Parameters.new(user: {}))
}.to raise_error(ActionController::ParameterMissing)
end

it "inherits builder methods from UserParameters" do
result = subject.with_password("new-password")

expect(result).to equal(subject)
expect(subject["password"]).to eq("new-password")
end
end
29 changes: 27 additions & 2 deletions spec/values/supervisor_parameters_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
require "rails_helper"

RSpec.describe SupervisorParameters do
# TODO: Add tests for SupervisorParameters
subject { described_class.new(params) }

pending "add some tests for SupervisorParameters"
let(:params) {
ActionController::Parameters.new(
supervisor: ActionController::Parameters.new(
email: "supervisor@example.com",
display_name: "Supervisor Name"
)
)
}

it "wraps params under the supervisor root key" do
expect(subject["email"]).to eq("supervisor@example.com")
expect(subject["display_name"]).to eq("Supervisor Name")
end

it "raises when the supervisor key is missing" do
expect {
described_class.new(ActionController::Parameters.new(user: {}))
}.to raise_error(ActionController::ParameterMissing)
end

it "inherits builder methods from UserParameters" do
result = subject.with_password("new-password")

expect(result).to equal(subject)
expect(subject["password"]).to eq("new-password")
end
end
134 changes: 132 additions & 2 deletions spec/values/user_parameters_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,137 @@
require "rails_helper"

RSpec.describe UserParameters do
# TODO: Add tests for UserParameters
subject { described_class.new(params) }

pending "add some tests for UserParameters"
let(:params) {
ActionController::Parameters.new(
user: ActionController::Parameters.new(
email: "user@example.com",
casa_org_id: 1,
display_name: "User Name",
phone_number: "1234567890",
date_of_birth: "2000-01-01",
password: "password123",
active: "1",
receive_reimbursement_email: "1",
type: "Volunteer",
monthly_learning_hours_report: "1",
address_attributes: {id: 1, content: "123 Main St"}
)
)
}

it "permits the allowed user attributes" do
aggregate_failures do
expect(subject["email"]).to eq("user@example.com")
expect(subject["casa_org_id"]).to eq(1)
expect(subject["display_name"]).to eq("User Name")
expect(subject["phone_number"]).to eq("1234567890")
expect(subject["date_of_birth"]).to eq("2000-01-01")
expect(subject["password"]).to eq("password123")
expect(subject["active"]).to eq("1")
expect(subject["receive_reimbursement_email"]).to eq("1")
expect(subject["type"]).to eq("Volunteer")
expect(subject["monthly_learning_hours_report"]).to eq("1")
expect(subject["address_attributes"].to_h).to eq("id" => 1, "content" => "123 Main St")
end
end

it "filters out attributes that are not permitted" do
params[:user][:admin] = true
expect(subject["admin"]).to be_nil
end

it "raises when the user key is missing" do
expect {
described_class.new(ActionController::Parameters.new(other: {}))
}.to raise_error(ActionController::ParameterMissing)
end

describe "#with_organization" do
let(:organization) { build_stubbed(:casa_org) }

it "sets casa_org_id to the organization's id and returns self" do
result = subject.with_organization(organization)

expect(result).to equal(subject)
expect(subject["casa_org_id"]).to eq(organization.id)
end
end

describe "#with_password" do
it "sets the password and returns self" do
result = subject.with_password("new-password")

expect(result).to equal(subject)
expect(subject["password"]).to eq("new-password")
end
end

describe "#with_type" do
it "sets the type and returns self" do
result = subject.with_type("Supervisor")

expect(result).to equal(subject)
expect(subject["type"]).to eq("Supervisor")
end
end

describe "#without_type" do
it "removes the type key and returns self" do
result = subject.without_type

expect(result).to equal(subject)
expect(subject.key?("type")).to be false
end
end

describe "#without_active" do
it "removes the active key and returns self" do
result = subject.without_active

expect(result).to equal(subject)
expect(subject.key?("active")).to be false
end
end

describe "#with_only" do
it "slices params down to only the given keys and returns self" do
result = subject.with_only(:email, :type)

expect(result).to equal(subject)
expect(subject.keys).to contain_exactly("email", "type")
end
end

describe "#without" do
# NOTE: `reject!` filters by comparing the given keys against the block's
# `key` argument, which ActionController::Parameters yields as a string.
# Symbols never match a string via `Array#include?`, so calling
# `without(:active)` is a no-op -- only string keys actually get removed.
# CreateCasaAdminService#build calls `without(:active, :type)` with symbols
# and is affected. Characterizing current behavior; fix tracked in #7067.
it "does not remove the key when given a symbol" do
subject.without(:active)

expect(subject["active"]).to eq("1")
end

it "removes the key when given a string" do
subject.without("active")

expect(subject["active"]).to be_nil
end

# NOTE: unlike the other builder methods, `without` returns the result of
# `reject!` (the underlying ActionController::Parameters), not `self` --
# so it does not return a UserParameters instance and can't be chained
# with the other `with_*`/`without_*` methods afterward. Also part of #7067.
it "returns the underlying params object rather than the UserParameters wrapper" do
result = subject.without("active")

expect(result).to be_a(ActionController::Parameters)
expect(result).not_to be_a(described_class)
end
end
end
Loading