Skip to content
This repository has been archived by the owner on May 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #7 from guardian/add-get-without-default
Browse files Browse the repository at this point in the history
Remove implicit in config and add method to check if store is populated
  • Loading branch information
philmcmahon committed Jul 18, 2016
2 parents 1b981ab + e10f5cd commit 2e6a21f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
Expand Up @@ -12,9 +12,9 @@ trait PermissionsProvider {

implicit lazy val executionContext: ExecutionContext = scala.concurrent.ExecutionContext.Implicits.global

implicit def config: PermissionsConfig
def config: PermissionsConfig

private val store: PermissionsStore = new PermissionsStore
val store: PermissionsStore = new PermissionsStore(config)

def get(perm: Permission)(implicit user: PermissionsUser): Future[PermissionAuthorisation] =
store.get(perm).recover {
Expand Down
Expand Up @@ -32,15 +32,15 @@ object PermissionsStoreModel {
}


class PermissionsStore(provider: Option[PermissionsStoreProvider] = None)(implicit config: PermissionsConfig, executionContext: ExecutionContext) {
class PermissionsStore(config: PermissionsConfig, provider: Option[PermissionsStoreProvider] = None)(implicit executionContext: ExecutionContext) {

val storeProvider: PermissionsStoreProvider = provider.getOrElse(new PermissionsStoreFromS3)
val storeProvider: PermissionsStoreProvider = provider.getOrElse(new PermissionsStoreFromS3(config))

def get(perm: Permission)(implicit user: PermissionsUser, config: PermissionsConfig): Future[PermissionAuthorisation] =
def get(perm: Permission)(implicit user: PermissionsUser): Future[PermissionAuthorisation] =
list.map(_.getOrElse(perm, perm.defaultValue))


def list(implicit user: PermissionsUser, config: PermissionsConfig): Future[PermissionsMap] = {
def list(implicit user: PermissionsUser): Future[PermissionsMap] = {
if (config.enablePermissionsStore)
storeProvider.store.future()
.flatMap {
Expand All @@ -57,21 +57,29 @@ class PermissionsStore(provider: Option[PermissionsStoreProvider] = None)(implic

trait PermissionsStoreProvider {
val store: Agent[PermissionsStoreModel]
def storeIsEmpty: Boolean
}


private[client] final class PermissionsStoreFromS3(refreshFrequency: Option[FiniteDuration] = Some(Duration(1, MINUTES)),
s3Client: Option[AmazonS3] = None)
(implicit config: PermissionsConfig,
actorSystem: ActorSystem = ActorSystem(),
executionContext: ExecutionContext) extends PermissionsStoreProvider {
private[client] final class PermissionsStoreFromS3(config: PermissionsConfig,
refreshFrequency: Option[FiniteDuration] = Some(Duration(1, MINUTES)),
s3Client: Option[AmazonS3] = None)
(implicit actorSystem: ActorSystem = ActorSystem(),
executionContext: ExecutionContext) extends PermissionsStoreProvider {

implicit lazy val logger = LoggerFactory.getLogger(getClass)

implicit private val timeout = Timeout(Duration(5, SECONDS))

val store: Agent[PermissionsStoreModel] = Agent(PermissionsStoreModel.empty)

def storeIsEmpty = {
store.get() match {
case PermissionsStoreModel.empty => true
case s: PermissionsStoreModel => false
}
}

private val s3 = s3Client.getOrElse {
new AmazonS3(creds = config.awsCredentials, region = config.s3Region)
}
Expand Down
Expand Up @@ -13,7 +13,7 @@ import scala.concurrent.ExecutionContext.Implicits.global

class PermissionsStoreTest extends FunSuite with Matchers with MockitoSugar with BeforeAndAfterAll with ScalaFutures {

implicit val config = PermissionsConfig("composer", Seq.empty)
val config = PermissionsConfig("composer", Seq.empty)

val testJson =
"""
Expand Down Expand Up @@ -53,7 +53,7 @@ class PermissionsStoreTest extends FunSuite with Matchers with MockitoSugar with

test("should parse from S3") {
mockS3Response(testJson)
val store = new PermissionsStoreFromS3(refreshFrequency = None, s3Client = Some(s3Mock))
val store = new PermissionsStoreFromS3(config, refreshFrequency = None, s3Client = Some(s3Mock))
val result = store.get

result.defaults.head should be(launchContentPermission)
Expand Down Expand Up @@ -82,7 +82,7 @@ class PermissionsStoreTest extends FunSuite with Matchers with MockitoSugar with

test("should refresh store from S3") {
mockS3Response(testJson)
val storeProvider = new PermissionsStoreFromS3(refreshFrequency = None, s3Client = Some(s3Mock))
val storeProvider = new PermissionsStoreFromS3(config, refreshFrequency = None, s3Client = Some(s3Mock))

val initStore = storeProvider.store.get()
initStore.defaults should be(Seq.empty)
Expand Down Expand Up @@ -123,8 +123,8 @@ class PermissionsStoreTest extends FunSuite with Matchers with MockitoSugar with
test("should fallback to defaults when overrides not present on a permissions list") {
mockS3Response(testJson)

val provider = new PermissionsStoreFromS3(refreshFrequency = None, s3Client = Some(s3Mock))
val store = new PermissionsStore(Some(provider))
val provider = new PermissionsStoreFromS3(config, refreshFrequency = None, s3Client = Some(s3Mock))
val store = new PermissionsStore(config, Some(provider))

provider.refreshStore.futureValue

Expand Down Expand Up @@ -154,8 +154,8 @@ class PermissionsStoreTest extends FunSuite with Matchers with MockitoSugar with
""".stripMargin
}

val provider = new PermissionsStoreFromS3(refreshFrequency = None, s3Client = Some(s3Mock))
val store = new PermissionsStore(Some(provider))
val provider = new PermissionsStoreFromS3(config, refreshFrequency = None, s3Client = Some(s3Mock))
val store = new PermissionsStore(config, Some(provider))

provider.refreshStore.futureValue

Expand All @@ -177,8 +177,8 @@ class PermissionsStoreTest extends FunSuite with Matchers with MockitoSugar with

when(s3Mock.getContentsAndLastModified(config.s3PermissionsFile, s"${config.s3Bucket}/${config.s3BucketPrefix}")).thenThrow(new AmazonServiceException("Mocked AWS Exception"))

val provider = new PermissionsStoreFromS3(s3Client = Some(s3Mock))
val store = new PermissionsStore(Some(provider))
val provider = new PermissionsStoreFromS3(s3Client = Some(s3Mock), config = config)
val store = new PermissionsStore(config, Some(provider))

implicit val user = PermissionsUser("james")

Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/example/MyPermissions.scala
Expand Up @@ -8,7 +8,7 @@ import scala.concurrent.Future
object MyPermissions extends PermissionsProvider {
val app = "composer"

implicit def config = PermissionsConfig(
def config = PermissionsConfig(
app = app,
all = all
)
Expand Down

0 comments on commit 2e6a21f

Please sign in to comment.