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
131 changes: 91 additions & 40 deletions spec/values/user_parameters_spec.rb
Original file line number Diff line number Diff line change
@@ -1,75 +1,126 @@
require "rails_helper"

RSpec.describe UserParameters do
def build_params(attrs)
ActionController::Parameters.new(user: attrs)
subject { described_class.new(params) }

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

describe "#without" do
it "removes the given keys and is chainable" do
params = build_params(display_name: "Jane", active: "false", type: "Volunteer")

result = described_class.new(params).without(:active, :type)
it "filters out attributes that are not permitted" do
params[:user][:admin] = true
expect(subject["admin"]).to be_nil
end

expect(result).to be_a(described_class)
expect(result.to_h).to eq("display_name" => "Jane")
end
it "raises when the user key is missing" do
expect {
described_class.new(ActionController::Parameters.new(other: {}))
}.to raise_error(ActionController::ParameterMissing)
end

it "removes keys even when the params only has string keys" do
params = build_params(display_name: "Jane", active: "false")
describe "#with_organization" do
let(:organization) { build_stubbed(:casa_org) }

result = described_class.new(params).without(:active)
it "sets casa_org_id to the organization's id and returns self" do
result = subject.with_organization(organization)

expect(result.to_h).to eq("display_name" => "Jane")
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")

it "supports further chaining after without" do
params = build_params(display_name: "Jane", active: "false", password: "old")
expect(result).to equal(subject)
expect(subject["password"]).to eq("new-password")
end
end

result = described_class.new(params).without(:active).with_password("new-password")
describe "#with_type" do
it "sets the type and returns self" do
result = subject.with_type("Supervisor")

expect(result.to_h).to eq("display_name" => "Jane", "password" => "new-password")
expect(result).to equal(subject)
expect(subject["type"]).to eq("Supervisor")
end
end

describe "#without_type" do
it "removes the type key" do
params = build_params(display_name: "Jane", type: "Volunteer")
it "removes the type key and returns self" do
result = subject.without_type

result = described_class.new(params).without_type

expect(result.to_h).to eq("display_name" => "Jane")
expect(result).to equal(subject)
expect(subject.key?("type")).to be false
end
end

describe "#without_active" do
it "removes the active key" do
params = build_params(display_name: "Jane", active: "false")

result = described_class.new(params).without_active
it "removes the active key and returns self" do
result = subject.without_active

expect(result.to_h).to eq("display_name" => "Jane")
expect(result).to equal(subject)
expect(subject.key?("active")).to be false
end
end

describe "#with_organization" do
it "sets casa_org_id" do
params = build_params(display_name: "Jane")
organization = create(:casa_org)

result = described_class.new(params).with_organization(organization)
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_h).to eq("display_name" => "Jane", "casa_org_id" => organization.id)
expect(result).to equal(subject)
expect(subject.keys).to contain_exactly("email", "type")
end
end

describe "#with_password" do
it "sets password" do
params = build_params(display_name: "Jane")
describe "#without" do
it "removes the key when given a symbol" do
subject.without(:active)

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

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

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

result = described_class.new(params).with_password("secret123")
it "returns self so it can be chained" do
result = subject.without(:active)

expect(result.to_h).to eq("display_name" => "Jane", "password" => "secret123")
expect(result).to equal(subject)
end
end
end
Loading