Bug #12143
closedPlugin permissions not available during tests
Description
The security block is evaluated at rails start up, but the permissions aren't available in a test.
1. Install foreman_salt
2. Throw a binding.pry in app/services/foreman/plugin.rb
204: Permission.where(:name => name).first_or_create(:resource_type => options[:resource_type])
=> 205: binding.pry if name.to_s == 'view_salt_environments'
3. In a test, binding.pry there too
4. Run the test. You can confirm the Permission exists when the plugin is loaded, but in the actual test, it's no longer there
Something with transactions or fixtures?
Updated by Dominic Cleal about 9 years ago
Test databases are always cleaned and then fixtures loaded, so you'll need to add any permissions into fixtures too, or create them on the fly (Permission.create, FG), as it stands.
#11206 added a method into the plugin definition to return which roles and permissions were registered, so you may be able to use this to recreate the permissions - or test that they're correct.
Updated by Stephen Benjamin about 9 years ago
Oh, I didn't realize fixtures would reset the whole database, I figured it'd use transactions intelligently and keep the database at the initial state at the beginning of each test. I don't want to use fixtures in foreman_salt, but using FG works fine.
You can close this if you want.
Updated by Dominic Cleal about 9 years ago
It's less that fixtures cause the reset, just that Rails starts with an empty test database and either it, and/or database_cleaner cause the reset. Fixtures get loaded after, plus any setup mechanisms in the test (which could call FG).
I'll leave it open for now, unless you wish to close it, because I agree that Foreman could help more here, perhaps by (optionally?) populating permissions in the test setup. foreman_salt isn't the only plugin that has hit this problem when writing tests that depend on permissions, because I think it's been mentioned in foreman_discovery too.
Updated by Dominic Cleal about 8 years ago
- Related to Bug #16821: AccessPermissionsTest does not pick up plugin permissions added
Updated by Dominic Cleal about 8 years ago
- Status changed from New to Ready For Testing
#16821 added this functionality, permissions registered by plugins are now initialised in the test environment.
Updated by Dominic Cleal about 8 years ago
- Status changed from Ready For Testing to Resolved