56 lines
1.6 KiB
Ruby
56 lines
1.6 KiB
Ruby
# typed: false
|
|
require "rails_helper"
|
|
|
|
RSpec.describe ApplicationController, type: :controller do
|
|
# Creating a test controller to access protected methods
|
|
controller do
|
|
def index
|
|
render plain: "Test"
|
|
end
|
|
end
|
|
|
|
describe "#current_ip_address_role" do
|
|
let(:ip_address) { "192.168.1.1" }
|
|
|
|
# Execute a request before each test to initialize the controller
|
|
before { get :index }
|
|
|
|
context "when no IP role exists" do
|
|
before do
|
|
# Mock the remote_ip method to return our test IP
|
|
allow(controller.request).to receive(:remote_ip).and_return(ip_address)
|
|
# Mock the IpAddressRole.for_ip method to return nil for this IP
|
|
allow(IpAddressRole).to receive(:for_ip).with(ip_address).and_return(
|
|
nil,
|
|
)
|
|
end
|
|
|
|
it "returns nil" do
|
|
expect(controller.current_ip_address_role).to be_nil
|
|
end
|
|
end
|
|
|
|
context "when an IP role exists" do
|
|
let(:ip_role) { instance_double(IpAddressRole) }
|
|
|
|
before do
|
|
# Mock the remote_ip method to return our test IP
|
|
allow(controller.request).to receive(:remote_ip).and_return(ip_address)
|
|
# Mock the IpAddressRole.for_ip method to return the test role for this IP
|
|
allow(IpAddressRole).to receive(:for_ip).with(ip_address).and_return(
|
|
ip_role,
|
|
)
|
|
end
|
|
|
|
it "returns the IP role" do
|
|
expect(controller.current_ip_address_role).to eq(ip_role)
|
|
end
|
|
|
|
it "memoizes the result" do
|
|
expect(IpAddressRole).to receive(:for_ip).once.and_return(ip_role)
|
|
2.times { controller.current_ip_address_role }
|
|
end
|
|
end
|
|
end
|
|
end
|