Skip to content
This repository was archived by the owner on Sep 30, 2019. It is now read-only.

wip: Use cats instead of uscala #14

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions auth/src/it/scala/janstenpickle/vault/auth/UserPassIT.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package janstenpickle.vault.auth

import janstenpickle.scala.syntax.AsyncResultSyntax._
import janstenpickle.vault.core.VaultSpec
import janstenpickle.vault.manage.Auth
import org.scalacheck.{Gen, Prop}
Expand All @@ -23,20 +24,20 @@ class UserPassIT extends VaultSpec with ScalaCheck {
lazy val userAdmin = janstenpickle.vault.manage.UserPass(config)

def setupClient(client: String) = authAdmin.enable("userpass", Some(client))
.attemptRun(_.getMessage()) must beOk
.attemptRun must beRight

def setupUser(username: String, password: String, client: String) =
userAdmin.create(username, password, 30, None, client)
.attemptRun(_.getMessage())
.attemptRun

def removeClient(client: String) =
authAdmin.disable(client).attemptRun(_.getMessage()) must beOk
authAdmin.disable(client).attemptRun must beRight

def authPass = test((username, password, client, ttl) =>
setupClient(client) and
(setupUser(username, password, client) must beOk) and
(setupUser(username, password, client) must beRight) and
(underTest.authenticate(username, password, ttl, client)
.attemptRun(_.getMessage()) must beOk) and
.attemptRun must beRight) and
removeClient(client)
)

Expand All @@ -45,27 +46,27 @@ class UserPassIT extends VaultSpec with ScalaCheck {
def badClient = test{ (username, password, client, ttl) =>
val badClientName = "nic-kim-cage-client"
setupClient(badClientName) and
(setupUser(username, password, client) must beFail) and
(setupUser(username, password, client) must beLeft) and
(underTest.authenticate(username, password, ttl, client)
.attemptRun(_.getMessage()) must beFail) and
.attemptRun must beLeft) and
removeClient(badClientName)
}

def badUser = test{ (username, password, client, ttl) =>
val badUserName = "nic-kim-cage-user"
setupClient(client) and
(setupUser(username, password, client) must beOk) and
(setupUser(username, password, client) must beRight) and
(underTest.authenticate(badUserName, password, ttl, client)
.attemptRun(_.getMessage()) must beFail) and
.attemptRun must beLeft) and
removeClient(client)
}

def badPassword = test{ (username, password, client, ttl) =>
val badPasswordValue = "nic-kim-cage-password"
setupClient(client) and
(setupUser(username, password, client) must beOk) and
(setupUser(username, password, client) must beRight) and
(underTest.authenticate(username, badPasswordValue, ttl, client)
.attemptRun(_.getMessage()) must beFail) and
.attemptRun must beLeft) and
removeClient(client)
}

Expand Down
4 changes: 2 additions & 2 deletions auth/src/main/scala/janstenpickle/vault/auth/UserPass.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import janstenpickle.scala.syntax.AsyncResultSyntax._
import janstenpickle.scala.syntax.SyntaxRequest._
import janstenpickle.scala.syntax.ResponseSyntax._
import janstenpickle.vault.core.WSClient
import uscala.concurrent.result.AsyncResult
import janstenpickle.scala.result._

import scala.concurrent.ExecutionContext

Expand All @@ -16,7 +16,7 @@ case class UserPass(wsClient: WSClient) {
password: String,
ttl: Int,
client: String = "userpass"
)(implicit ec: ExecutionContext): AsyncResult[String, UserPassResponse] =
)(implicit ec: ExecutionContext): AsyncResult[UserPassResponse] =
wsClient.path(s"auth/$client/login/$username").
post(Map("password" -> password, "ttl" -> s"${ttl}s")).
toAsyncResult.
Expand Down
18 changes: 11 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import sbt.Keys._

name := "vault"

lazy val uscalaVersion = "0.5.1"
lazy val scalaVersion211 = "2.11.11"
lazy val scalaVersion212 = "2.12.4"

lazy val specs2Version = "3.8.8"
lazy val circeVersion = "0.7.0"
lazy val catsVersion = "1.0.1"
lazy val circeVersion = "0.9.0"
lazy val dispatchVersion = "0.11.3"
lazy val startVaultTask = TaskKey[Unit](
"startVaultTask",
Expand Down Expand Up @@ -38,7 +41,8 @@ val pomInfo = (

lazy val commonSettings = Seq(
version := "0.4.2-SNAPSHOT",
scalaVersion := "2.11.11",
scalaVersion := scalaVersion211,
crossScalaVersions := Seq(scalaVersion211, scalaVersion212),
organization := "janstenpickle.vault",
pomExtra := pomInfo,
autoAPIMappings := true,
Expand All @@ -50,11 +54,9 @@ lazy val commonSettings = Seq(
url("https://github.com/janstenpickle/scala-vault/blob/master/LICENSE")
),
resolvers ++= Seq(Resolver.sonatypeRepo("releases"), Resolver.jcenterRepo),
libraryDependencies += "org.typelevel" %% "cats-core" % catsVersion,
libraryDependencies ++= Seq(
"net.databinder.dispatch" %% "dispatch-core" % dispatchVersion,
"org.uscala" %% "uscala-result" % uscalaVersion,
"org.uscala" %% "uscala-result-async" % uscalaVersion,
"org.uscala" %% "uscala-result-specs2" % uscalaVersion % "it,test",
"org.specs2" %% "specs2-core" % specs2Version % "it,test",
"org.specs2" %% "specs2-scalacheck" % specs2Version % "it,test",
"org.specs2" %% "specs2-junit" % specs2Version % "it,test"
Expand All @@ -77,7 +79,9 @@ lazy val commonSettings = Seq(
"-unchecked",
"-deprecation",
"-feature",
"-language:implicitConversions"),
"-language:implicitConversions",
"-Ypartial-unification"
),
javacOptions in Compile ++= Seq(
"-source", "1.8",
"-target", "1.8",
Expand Down
26 changes: 14 additions & 12 deletions core/src/it/scala/janstenpickle/vault/core/SecretsIT.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package janstenpickle.vault.core

import janstenpickle.scala.syntax.AsyncResultSyntax._
import janstenpickle.scala.result._
import org.scalacheck.Prop
import org.specs2.ScalaCheck

Expand Down Expand Up @@ -33,20 +35,20 @@ trait SecretsTests extends VaultSpec with ScalaCheck {
lazy val badServer = Secrets(badServerConfig, backend)

def set = Prop.forAllNoShrink(strGen, strGen) { (key, value) =>
good.set(key, value).attemptRun(_.getMessage()) must beOk
good.set(key, value).attemptRun must beRight
}

def get = Prop.forAllNoShrink(strGen, strGen) { (key, value) =>
(good.set(key, value).attemptRun(_.getMessage()) must beOk) and
(good.get(key).attemptRun(_.getMessage()) must beOk.like {
(good.set(key, value).attemptRun must beRight) and
(good.get(key).attemptRun must beRight.like {
case a => a === value
})
}

def list = Prop.forAllNoShrink(strGen, strGen, strGen) { (key1, key2, value) =>
(good.set(key1, value).attemptRun(_.getMessage()) must beOk) and
(good.set(key2, value).attemptRun(_.getMessage()) must beOk) and
(good.list.attemptRun(_.getMessage()) must beOk[List[String]].like {
(good.set(key1, value).attemptRun must beRight) and
(good.set(key2, value).attemptRun must beRight) and
(good.list.attemptRun must beRight[List[String]].like {
case a => a must containAllOf(Seq(key1, key2))
})
}
Expand All @@ -56,29 +58,29 @@ trait SecretsTests extends VaultSpec with ScalaCheck {
good.set(
"nicolas-cage",
Map(key1 -> value1, key2 -> value2)
).attemptRun(_.getMessage()) must beOk
).attemptRun must beRight
}

def getSetMulti = Prop.forAllNoShrink(
strGen, strGen, strGen, strGen, strGen
) { (key1, key2, value1, value2, mainKey) =>
val testData = Map(key1 -> value1, key2 -> value2)
(good.set(mainKey, testData).attemptRun(_.getMessage()) must beOk) and
(good.getAll(mainKey).attemptRun(_.getMessage()) must beOk.like {
(good.set(mainKey, testData).attemptRun must beRight) and
(good.getAll(mainKey).attemptRun must beRight.like {
case a => a === testData
})
}

def failGet = good.get("john").attemptRun(_.getMessage()) must beFail.
def failGet = good.get("john").attemptRun must beLeft.
like { case err =>
err must contain("Received failure response from server: 404")
}

def failSetBadServer = badServer.set(
"nic", "cage"
).attemptRun(_.getMessage()) must beFail
).attemptRun must beLeft

def failSetBadToken = badToken.set(
"nic", "cage"
).attemptRun(_.getMessage()) must beFail
).attemptRun must beLeft
}
14 changes: 8 additions & 6 deletions core/src/it/scala/janstenpickle/vault/core/VaultSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@ package janstenpickle.vault.core

import java.net.URL

import janstenpickle.scala.result._
import janstenpickle.scala.syntax.AsyncResultSyntax._
import janstenpickle.scala.syntax.SyntaxRequest._
import janstenpickle.scala.syntax.ResponseSyntax._
import janstenpickle.scala.syntax.VaultConfigSyntax._
import org.scalacheck.Gen
import org.specs2.Specification
import org.specs2.matcher.EitherMatchers
import org.specs2.specification.core.Fragments
import uscala.result.specs2.ResultMatchers

import scala.concurrent.ExecutionContext
import scala.io.Source


trait VaultSpec extends Specification with ResultMatchers {
trait VaultSpec extends Specification with EitherMatchers {
implicit val errConverter: Throwable => String = _.getMessage
implicit val ec: ExecutionContext = ExecutionContext.global

lazy val rootToken = Source.fromFile("/tmp/.root-token").mkString.trim
lazy val roleId = Source.fromFile("/tmp/.role-id").mkString.trim
lazy val secretId = Source.fromFile("/tmp/.secret-id").mkString.trim
lazy val rootToken: String = Source.fromFile("/tmp/.root-token").mkString.trim
lazy val roleId: String = Source.fromFile("/tmp/.role-id").mkString.trim
lazy val secretId: String = Source.fromFile("/tmp/.secret-id").mkString.trim

lazy val rootConfig: VaultConfig = VaultConfig(
WSClient(new URL("http://localhost:8200")), rootToken
Expand All @@ -38,7 +40,7 @@ trait VaultSpec extends Specification with ResultMatchers {
"con-air"
)

def check = config.token.attemptRun(_.getMessage) must beOk
def check = config.token.attemptRun should beRight

override def map(fs: => Fragments) =
s2"""
Expand Down
30 changes: 30 additions & 0 deletions core/src/main/scala/janstenpickle/scala/result/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package janstenpickle.scala

import scala.concurrent.Future
import cats.implicits._


import scala.language.postfixOps

package object result {
type Result[R] = Either[String, R]
val Result: Either.type = Either

implicit class ResultCompanionOps(dc: Either.type ){
def pure[R](r: R): Either[String, R] = Either right r

def fail[R](f: String): Either[String, R] = Either left f
}

type AsyncResult[R] = Future[Result[R]]

type AsyncEitherT[R] = cats.data.EitherT[Future, String, R]

object AsyncResult {
def pure[R](r: R): AsyncResult[R] = {
Future.successful(Either right r)
}
}


}
Loading