Lately I have used an outside in approach to building my web application in ruby on rails. What that means is that I am driving my application development by building integration tests. I picked up this technique a year or so ago from thoughtbot. So far, i have really enjoyed the insight it has given me and how much less code I write than when i was doing things by building models first.
Many of the applications I end up building as a consultant are multi-tenant application. So there are many customers that have to live in strict or limited scope from each other. I have built a really simple helper that I have been using and wanted to share it below in my TestCase class description
class ActiveSupport::TestCaseActiveRecord::Migration.check_pending! include Warden::Test::Helpers fixtures :all def as_user(user) login_as users(user.to_sym) yield logout end end
It is super simple and I was a little hesitant to put it out there. All it does is a login the user, execute the block that is passed in and then log the user out. In a multitenant app though it is really imporant to check your scopes based on the type of user that is logging in (admin, super admin, org admin, user, anonymous, etc). On top of that it adds to some very understandable tests. Ozark and nixa are just two random cities in my area.
scenario "Employee cannot visit other organizations tickets" do as_user(:ozark_employee) do ozark_ticket = organizations(:ozark).tickets.first visit ticket_path(ozark_ticket) page.text.must_include unauthorized_error end end scenario "Can edit own tickets" do as_user(:ozark_employee) do visit tickets_path click_link 'Edit' fill_in 'Description', :with => 'Some new description xxx' click_button 'Update Ticket' page.text.must_include 'successfully updated' page.text.must_include 'Some new description xxx' end end
This one simple helper has really tied in with my multitenant outside in development and sped up my dev time when building out how different types of users should experience an application.