Skip to content

Commit

Permalink
Merge pull request #24 from hmrc/PE-3136
Browse files Browse the repository at this point in the history
PE-3136 Adding feature flag functionality and a feature flag for the …
  • Loading branch information
fenallen authored Jul 14, 2017
2 parents 17226e9 + 477bba8 commit 90c075a
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 7 deletions.
43 changes: 43 additions & 0 deletions app/uk/gov/hmrc/openidconnect/userinfo/config/FeatureSwitch.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2017 HM Revenue & Customs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package uk.gov.hmrc.openidconnect.userinfo.config

case class FeatureSwitch(name: String, isEnabled: Boolean)


object FeatureSwitch {

def forName(name: String) = FeatureSwitch(name, java.lang.Boolean.getBoolean(systemPropertyName(name)))

def enable(switch: FeatureSwitch): FeatureSwitch = setProp(switch.name, true)

def disable(switch: FeatureSwitch): FeatureSwitch = setProp(switch.name, false)

private def setProp(name: String, value: Boolean): FeatureSwitch = {
sys.props += ((systemPropertyName(name), value.toString))
forName(name)
}

private def systemPropertyName(name: String) = s"feature.$name"

}

object UserInfoFeatureSwitches {

def countryCode = FeatureSwitch.forName("countryCode")

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,30 @@ package uk.gov.hmrc.openidconnect.userinfo.data

import org.joda.time._
import org.scalacheck.Gen
import uk.gov.hmrc.openidconnect.userinfo.config.UserInfoFeatureSwitches
import uk.gov.hmrc.openidconnect.userinfo.domain.{Address, Enrolment, EnrolmentIdentifier, GovernmentGatewayDetails, UserInfo}
import uk.gov.hmrc.play.http.Token

trait UserInfoGenerator {
val firstNames = List(Some("Roland"), Some("Eddie"), Some("Susanna"), Some("Jake"), Some("Oy"), Some("Cuthbert"), Some("Alain"), Some("Jamie"), Some("Thomas"), Some("Susan"), Some("Randall"), None)
val middleNames = List(Some("De"), Some("Donald"), Some("Billy"), Some("E"), Some("Alex"), Some("Abel"), None, None, None, None, None, None)
val lastNames = List(Some("Deschain"), Some("Dean"), Some("Dean"), Some("Chambers"), Some("Bumbler"), Some("Allgood"), Some("Johns"), Some("Curry"), Some("Whitman"), Some("Delgado"), Some("Flagg"), Some("Bowen"), None)
val address = Some(Address(

val addressWithCountryCode = Some(Address(
"""221B Baker Street
|London
|NW1 9NT
|Great Britain""".stripMargin, Some("NW1 9NT"), Some("Great Britain"), Some("GB")))
|Great Britain
|GB""".stripMargin, Some("NW1 9NT"), Some("Great Britain"), Some("GB")))

val addressWithoutCountryCode = Some(Address(
"""221B Baker Street
|London
|NW1 9NT
|Great Britain""".stripMargin, Some("NW1 9NT"), Some("Great Britain"), None))

def address = if (UserInfoFeatureSwitches.countryCode.isEnabled) {addressWithCountryCode} else {addressWithoutCountryCode}

val enrolments = Seq(Enrolment("IR-SA", List(EnrolmentIdentifier("UTR", "174371121"))))
val government_gateway: GovernmentGatewayDetails = GovernmentGatewayDetails(Some("32131"),Some(Token("ggToken")),Some("User"),Some("affinityGroup"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package uk.gov.hmrc.openidconnect.userinfo.services

import org.joda.time.LocalDate
import uk.gov.hmrc.openidconnect.userinfo.config.{FeatureSwitch, UserInfoFeatureSwitches}
import uk.gov.hmrc.openidconnect.userinfo.domain.{Address, Authority, Country, DesAddress, DesUserInfo, Enrolment, GovernmentGatewayDetails, UserDetails, UserInfo}
import uk.gov.hmrc.play.http.Token

Expand All @@ -31,8 +32,9 @@ trait UserInfoTransformer {
def address = if (scopes.contains("address")) {
val country = desUserInfo flatMap (u => u.address.countryCode flatMap countryService.getCountry)
val countryName = country flatMap {c => c.shortName}
val countryCode = country flatMap {c => c.alphaTwoCode}
val countryCode = if (UserInfoFeatureSwitches.countryCode.isEnabled){ country flatMap { c => c.alphaTwoCode} } else None
desUserInfo map (u => Address(formattedAddress(u.address, country), u.address.postcode, countryName, countryCode))

} else None

val identifier = if (scopes.contains("openid:gov-uk-identifiers")) authority flatMap {a => a.nino map {n => n}} else None
Expand All @@ -58,7 +60,7 @@ trait UserInfoTransformer {

private def formattedAddress(desAddress: DesAddress, country: Option[Country]) = {
val countryName = country flatMap {c => c.shortName}
val countryCode = country flatMap {c => c.alphaTwoCode}
val countryCode = if (UserInfoFeatureSwitches.countryCode.isEnabled){ country flatMap { c => c.alphaTwoCode} } else None
Seq(desAddress.line1, desAddress.line2, desAddress.line3, desAddress.line4, desAddress.line5, desAddress.postcode, countryName, countryCode).flatten.mkString("\n")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,41 @@ package unit.uk.gov.hmrc.openidconnect.userinfo.data
import uk.gov.hmrc.openidconnect.userinfo.data.UserInfoGenerator
import uk.gov.hmrc.openidconnect.userinfo.domain.UserInfo
import org.joda.time.LocalDate
import org.scalatest.BeforeAndAfterEach
import org.scalatest.prop.PropertyChecks
import uk.gov.hmrc.openidconnect.userinfo.config.{FeatureSwitch, UserInfoFeatureSwitches}
import uk.gov.hmrc.play.test.UnitSpec

class UserInfoGeneratorSpec extends UnitSpec with PropertyChecks {
class UserInfoGeneratorSpec extends UnitSpec with PropertyChecks with BeforeAndAfterEach {
val ninoPattern = "^[A-CEGHJ-NOPR-TW-Z]{2}[0-9]{6}[ABCD\\s]{1}$".r
val from = new LocalDate(1939, 12, 27)
val until = new LocalDate(1998, 12, 29)


override protected def beforeEach(): Unit = {
FeatureSwitch.enable(UserInfoFeatureSwitches.countryCode)
}

"userInfo" should {
"generate an OpenID Connect compliant UserInfo response" in forAll(UserInfoGenerator.userInfo) { userInfo: UserInfo =>
UserInfoGenerator.firstNames should contain (userInfo.given_name)
UserInfoGenerator.middleNames should contain (userInfo.middle_name)
UserInfoGenerator.lastNames should contain (userInfo.family_name)
userInfo.address shouldBe UserInfoGenerator.address
userInfo.address shouldBe UserInfoGenerator.addressWithCountryCode
assertValidDob(userInfo.birthdate.getOrElse(fail(s"Generated user's dob is not defined")))
assertValidNino(userInfo.uk_gov_nino.getOrElse(fail(s"Generated user's NINO is not defined")))
}
}

"userInfo" should {
"generate an OpenID Connect compliant UserInfo response without country code when feature flag is disabled" in forAll( {
FeatureSwitch.disable(UserInfoFeatureSwitches.countryCode)
UserInfoGenerator.userInfo } ) { userInfo: UserInfo =>
userInfo.address shouldBe UserInfoGenerator.addressWithoutCountryCode
}
}


private def assertValid(name: String, expected: List[String], actual: Option[String]): Unit = {
expected should contain (actual.getOrElse(fail(s"Generated user's $name is not defined")))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ package unit.uk.gov.hmrc.openidconnect.userinfo.services

import org.joda.time.LocalDate
import org.mockito.BDDMockito.given
import org.scalatest.BeforeAndAfterEach
import org.scalatest.mock.MockitoSugar
import uk.gov.hmrc.domain.Nino
import uk.gov.hmrc.openidconnect.userinfo.config.{FeatureSwitch, UserInfoFeatureSwitches}
import uk.gov.hmrc.openidconnect.userinfo.domain._
import uk.gov.hmrc.openidconnect.userinfo.services.{CountryService, UserInfoTransformer}
import uk.gov.hmrc.play.http.{HeaderCarrier, Token}
import uk.gov.hmrc.play.test.UnitSpec

import scala.concurrent.ExecutionContext.Implicits.global

class UserInfoTransformerSpec extends UnitSpec with MockitoSugar {
class UserInfoTransformerSpec extends UnitSpec with MockitoSugar with BeforeAndAfterEach {

val ukCountryCode = 10
val nino = Nino("AB123456A")
val desAddress: DesAddress = DesAddress(Some("1 Station Road"), Some("Town Centre"), Some("London"), Some("England"), Some("UK"), Some("NW1 6XE"), Some(ukCountryCode))
Expand Down Expand Up @@ -58,6 +61,11 @@ class UserInfoTransformerSpec extends UnitSpec with MockitoSugar {
Some(government_gateway)
)


override protected def beforeEach() = {
FeatureSwitch.enable(UserInfoFeatureSwitches.countryCode)
}

trait Setup {
implicit val hc: HeaderCarrier = HeaderCarrier()

Expand Down Expand Up @@ -193,5 +201,16 @@ class UserInfoTransformerSpec extends UnitSpec with MockitoSugar {
val userInfoMissingPostCode = userInfo.copy(address = Some(userAddress.copy(formatted = "1 Station Road\nTown Centre\nLondon\nEngland\nUK\nUnited Kingdom\nGB", postal_code = None)), hmrc_enrolments = None, email = None)
result shouldBe userInfoMissingPostCode
}

"not return country code when feature flag is off" in new Setup {

FeatureSwitch.disable(UserInfoFeatureSwitches.countryCode)
val scopes = Set("address", "profile", "openid:gov-uk-identifiers", "openid:hmrc_enrolments", "openid:government_gateway", "email")
val result = await(transformer.transform(scopes, Some(desUserInfo), Some(enrolments), Some(authority), Some(userDetails), Some(ggToken)))

val userInfoMissingCountryCode = userInfo.copy(address = Some(userAddress.copy(formatted = "1 Station Road\nTown Centre\nLondon\nEngland\nUK\nNW1 6XE\nUnited Kingdom", code = None)))
result shouldBe userInfoMissingCountryCode
}

}
}

0 comments on commit 90c075a

Please sign in to comment.