Testing With Tenants 🤔
I have a simple Project model with fixtures like this:
one: name: MyString description: MyText notes: MyText status: green state: user: two account: company two: name: MyString2 description: MyText2 notes: MyText2 status: red state: user: one account: company
In my app I will have all tenant records restricted to company accounts.
And my tests (truncated):
And my tests (truncated):
require "test_helper" class ProjectsControllerTest < ActionDispatch::IntegrationTest setup do sign_in users(:two) switch_account(accounts(:company)) @project = projects(:one) end test "should show project" do get project_url(@project) assert_response :success end end
The create and index work fine. I am adding the user sign-in (with the Devise test helpers) and then adding the switch account to ensure that I am in the right account. My tests fail like this:
ERROR ProjectsControllerTest#test_should_show_project (3.03s) Minitest::UnexpectedError: ActiveRecord::RecordNotFound: Couldn't find Project with 'id'=980190962 [WHERE "projects"."account_id" = $1] app/controllers/projects_controller.rb:86:in `set_project' lib/jumpstart/lib/jumpstart/account_middleware.rb:30:in `call' test/controllers/projects_controller_test.rb:30:in `block in <class:ProjectsControllerTest>'
The projects should be visible to user :two.
Now the crazy part - lets comments out the sign_in and account switch:
Now the crazy part - lets comments out the sign_in and account switch:
require "test_helper" class ProjectsControllerTest < ActionDispatch::IntegrationTest setup do # sign_in users(:two) # switch_account(accounts(:company)) # ActsAsTenant.current_tenant = accounts(:company_regular_user) @project = projects(:one) end test "should create project" do assert_difference("Project.count") do post projects_url, params: { project: { description: @project.description, name: @project.name, notes: @project.notes, status: @project.status, user_id: @project.user_id } } assert assigns(:project).valid?, "VALIDATIONS: #{assigns(:project).errors.full_messages}" end assert_redirected_to project_url(Project.last) end end
All tests pass except the create:
FAIL ProjectsControllerTest#test_should_create_project (5.32s) VALIDATIONS: ["Account must exist"] test/controllers/projects_controller_test.rb:24:in `block (2 levels) in <class:ProjectsControllerTest>' test/controllers/projects_controller_test.rb:22:in `block in <class:ProjectsControllerTest>'
I add that validations test to debug forms as often validations will silently fail tests and drive me crazy. That's where I am seeing the issue - the account_id is not being set except acts_as_tenant is suppose to do that for you which is obviously is in my previous test.
I have a similar system working only older legacy app and it works fine - I must be missing something obvious here but I can't see it.
I have a similar system working only older legacy app and it works fine - I must be missing something obvious here but I can't see it.
Notifications
You’re not receiving notifications from this thread.