Skip to content

Commit

Permalink
Merge pull request #10 from Sensis/enableToggle
Browse files Browse the repository at this point in the history
allow configuration to disable feature modification at run time
  • Loading branch information
antony committed Mar 26, 2013
2 parents f50c8ee + 07c945c commit 75fe057
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
4 changes: 4 additions & 0 deletions grails-app/conf/Config.groovy
@@ -1,5 +1,9 @@
// configuration for plugin testing - will not be included in the plugin zip

featuresConfig {
allowToggling = true
}

features {
alwaysOn {
enabled = true
Expand Down
Expand Up @@ -27,20 +27,32 @@ class FeatureSwitchAdminController {

}

def toggle(String feature) {

grailsApplication.config.features[feature].enabled = !grailsApplication.config.features[feature].enabled
redirect action: 'switches'
private def modifyFeature(String feature, boolean enabled, Closure renderSuccess) {
if (grailsApplication.config.featuresConfig.allowToggling) {
grailsApplication.config.features[feature].enabled = enabled
renderSuccess()
} else {
response.status = 403
render "Modifying features at run time has been disabled"
}
}

def toggle(String feature) {
boolean toggled = !grailsApplication.config.features[feature].enabled
modifyFeature feature, toggled, {
redirect action: 'switches'
}
}

def enable(String feature) {
grailsApplication.config.features[feature].enabled = true
render text: [(feature): 'enabled'] as JSON
modifyFeature feature, true, {
render text: [(feature): 'enabled'] as JSON
}
}

def disable(String feature) {
grailsApplication.config.features[feature].enabled = false
render text: [(feature): 'disabled'] as JSON
modifyFeature feature, false, {
render text: [(feature): 'disabled'] as JSON
}
}
}
@@ -0,0 +1,32 @@
package uk.co.desirableobjects.featureswitch

import spock.lang.Specification
import grails.test.mixin.TestFor
import spock.lang.Unroll

@Unroll
@TestFor(FeatureSwitchAdminController)
class FeatureSwitchAdminControllerSpec extends Specification {

def "ability to toggle features can be disabled" (boolean allowToggling, boolean startState, Closure endpoint, boolean endState, int responseCode) {
given:
def featureName = "theFeature"
grailsApplication.config.features[featureName].enabled = startState
and:
grailsApplication.config.featuresConfig.allowToggling = allowToggling
when:
endpoint(featureName)
then:
response.status == responseCode
and:
grailsApplication.config.features[featureName].enabled == endState
where:
allowToggling | startState | endpoint | endState | responseCode
false | true | {controller.disable(it)} | true | 403
false | false | {controller.enable(it)} | false | 403
false | true | {controller.toggle(it)} | true | 403
true | true | {controller.disable(it)} | false | 200
true | false | {controller.enable(it)} | true | 200
true | true | {controller.toggle(it)} | false | 302
}
}

0 comments on commit 75fe057

Please sign in to comment.