Controller action returns "Invalid Json" when using a Fakerequest from a spec2 test - scala

I am using playframework 2.6 and play-slick 0.8.0.
Action code:
def addCompany = Authenticated {
DBAction(parse.json) {
implicit rs => {
val newCompany = rs.request.body
val result = CompanyTable.insert(newCompany.as[Company])(rs.dbSession)
if(result > 0)
Ok("{\"id\":"+result+"}")
else
Ok("New company was not created.")
}
}
}
The Action is a composition of an Action that just checks for a valid session and the DBAction, which requires the request body to have a valid JSON object.
Test code:
"should create a Company from a Json request" in new InMemoryDB {
val newCompany = Company(name = "New Company1")
val fr = FakeRequest(POST, "/company")
.withSession(("email", "bob#villa.com"))
.withHeaders(CONTENT_TYPE -> "application/json")
.withJsonBody(Json.toJson(newCompany))
val action = controllers.CompanyController.addCompany
val result = action(fr).run
status(result) should be_==(OK)
(contentAsJson(result) \ "id").as[Long] should be_>(1L)
}
The InMemoryDB class is just a FakeApplication with a pre-populated in memory database.
The issue that I am having is that when the test runs the result is always a 400 with body content containing a message saying [Invalid Json]. When I call the service using curl with the same JSON body content, it works and the id is returned.

I decided to build a separate test project, and I used the activator to create a seed for the new project. I noticed that in the generated test that a different method of calling the action was used, so I switched my project to use this method. It worked, but I don't know why.
New code:
"should create a Company from a Json request" in new InMemoryDB {
val newCompany = Company(name = "New Company1")
val action = route(
FakeRequest(POST, "/company")
.withSession(("email", "bob#villa.com"))
.withHeaders(CONTENT_TYPE -> "application/json")
.withJsonBody(Json.toJson(newCompany))
).get
status(action) should be_==(OK)
(contentAsJson(action) \ "id").as[Long] should be_>(1L)
}
As you can see it uses a call to route instead of calling the controller.

Related

Retrofit2 post request is successful but I'm not able to retrieve token from response body

So I'm trying to making a login(post) request to an API (https://reqres.in/api/login) with retrofit 2. The connection was successful as the response code is 200, when I did the same on Postman I received a response which contains a token string which I want but in android studio when I log the response body it gives different output. I am new to kotlin so I think I must be doing something wrong while retrieving response.
Output I'm receiving:
Response{protocol=h2, code=200, message=, url=https://reqres.in/api/login}
Output I want (token field)
{
"token": "QpwL5tke4Pnpja7X4"
}
Retrofit Builder
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(apiUrl)
.build()
val api : reqresAPI = retrofit.create(reqresAPI::class.java)
API Interface
interface reqresAPI {
// FOR REFERENCE
// https://reqres.in/api/login
// ANNOTATE WITH POST TO SEND DATA
#POST(value = "login")
fun sendData(
#Body user: User
): Call<ResponseModel> // CALL IS USED TO MAKE AN API CALL
}
Response Model
class ResponseModel{
val token : String = ""
get() = field
}
User model with 2 parameters email and password
class User (
val email :String,
val password :String
)
Calling API
val call = api.sendData(user)
call.enqueue(object : Callback<ResponseModel>{
override fun onResponse(call: Call<ResponseModel>, response: Response<ResponseModel>) {
Log.d("LOGGER", response.toString())
}
override fun onFailure(call: Call<ResponseModel>, t: Throwable) {
Log.d("LOGGER", "ERROR "+t.message.toString())
}
})
Please change this
class ResponseModel{
val token : String = ""
get() = field
}
to this
class ResponseModel{
#SerializedName("token")
val token : String
}

Gatling error : Failed to build request: No attribute named 'jwtoken' is defined

I have this error when running my script in scala: "Failed to build request: No attribute named 'jwtoken' is defined " , i extract the value of jwtoken then i put it in a variable, then i want to read it in an other request, i have put 2 objects because i want run two scenario's :
class PPT_GET extends Simulation
{ val httpProtocol = http
.baseUrl("") // Here is the root for all relative URLs
val acteurscsv = csv("./src/test/resources/Data/ksp-acteurs.csv").circular
val jwt = "123"
object TokenGen {
val tokenGen = exec(http("access_token")
.post("https://URL1/token")
.header("Content-Type", "application/x-www-form-urlencoded")
.formParam("grant_type", value = "client_credentials")
.formParam("client_id", value = "PRTAPIPPD")
.formParam("client_secret", value = "xxxxxx")
.check(status is 200, jsonPath("$.access_token").saveAs("atoken")))
.exec(http("jwt_token")
.get("https://bsc-ppd.web.bpifrance.fr/mga/sps/apiauthsvc/policy/AT4JWT")
.header("Authorization", "Bearer ${atoken}")
.header("CorrelationID", "someID")
.header("UsercallerSecret", "user1")
.header("UsercallerID", "X1")
.header("Accept", "application/json")
.check(status is 200, jsonPath("$.JWTFull").saveAs("jwtoken"))) //here i extract the variable jwtoken
}
object Acteurs {
val acteurs =
pause(80)
.feed(acteurscsv)
.exec(http("ksp-acteurs")
.get("https://URL1/ksp-acteurs/${ACTCODE}")
.header("Authorization", "Bearer ${jwtoken}" ) // here where i have the erreor
.check(status is 200))
}
i have put 2 objects because i want run two scenario's
This is not how things work. A scenario is a virtual user journey and data is saved in each virtual user's memory space called Session.
So you can't save data with a check in one scenario and expect to find it in another one, because those scenarios would be executed by different virtual users.
You have to chain your requests in the same scenario, eg:
val scn = scenario("foo").exec(TokenGen.tokenGen, Acteurs.acteurs)
Then, if your goal is to generate one single token for your test, you can use a global reference and sequential scenarios:
var token: String = null
val generateToken = scenario("loadToken")
.exec(TokenGen.tokenGen)
.exec { session =>
token = session("jwtoken").as[String]
session
}
object Acteurs {
val acteurs =
feed(acteurscsv)
.exec(http("ksp-acteurs")
.get("https://URL1/ksp-acteurs/${ACTCODE}")
.header("Authorization", _ => s"Bearer $token" )
.check(status is 200))
}
setUp(
generateToken.inject(atOnceUsers(1))
.andThen(
scenario("acteurs").exec(Acteurs.acteurs).inject(???)
)
)

Modify contentHeaders of Swagger Codegen methods in kotlin

I'm using swagger codegen for my REST API calls. For authentication purposes i need to send a session-token within the headers of every request. This is currently done, via APIClients' defaultHeaders
open class ApiClient(val baseUrl: String) {
companion object {
...
#JvmStatic
var defaultHeaders: Map<String, String> by ApplicationDelegates.setOnce(mapOf(ContentType to JsonMediaType, Accept to JsonMediaType))
...
}
}
The way swagger generates the code, these headers can only be modified once.
ApiClient.defaultHeaders += mapOf("Authorization" to userSession!!.idToken.jwtToken)
The problem with this is, that i cannot change the token (e.g. because another user logged in within the application lifetime). Looking deeper into the generated code, before each request is sent, a merge of both defaultHeaders and requestConfig.headers (=contentHeaders) is being made.
inline protected fun <reified T: Any?> request(requestConfig: RequestConfig, body : Any? = null): ApiInfrastructureResponse<T?> {
...
val headers = defaultHeaders + requestConfig.headers
...
}
The given RequestConfig object comes from every api call. However it is not possible to change these contentHeaders. Also they are empty by default.
fun someAPIRestCall(someParam: kotlin.String) : Unit {
val localVariableBody: kotlin.Any? = type
val localVariableQuery: MultiValueMap = mapOf()
val contentHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf() // THESE WILL BE MERGED WITH defaultHeaders
val acceptsHeaders: kotlin.collections.Map<kotlin.String,kotlin.String> = mapOf("Accept" to "application/json")
val localVariableHeaders: kotlin.collections.MutableMap<kotlin.String,kotlin.String> = mutableMapOf()
localVariableHeaders.putAll(contentHeaders)
localVariableHeaders.putAll(acceptsHeaders)
val localVariableConfig = RequestConfig(
RequestMethod.POST,
"someEndpointURL"),
query = localVariableQuery,
headers = localVariableHeaders // THESE WILL BE MERGED WITH defaultHeaders
)
val response = request<Unit>(
localVariableConfig,
localVariableBody
)
...
}
Is it possible to tell swagger-codegen to include some kind of parameter to the generated method signature to add values to those contentHeaders?
EDIT:
This is the current code-gen call within my gradle build chain
task generateSwagger(type: JavaExec) {
main = "-jar"
args "swagger-codegen-cli-2.4.7.jar", "generate", "-i", "./swagger_core.yml", "-l", "kotlin", "-o", "./tmp/RestApi", "--type-mappings", "number=kotlin.Long"
}
By now, i found a solution, that is more of a hack, but it works.
As i am using gradle to build the app, i introduced a task, that changes the generated swagger code, before it actually compiles.
task editAPISources {
def token = "Map<String, String> by ApplicationDelegates.setOnce(mapOf(ContentType to JsonMediaType, Accept to JsonMediaType))"
def value = "MutableMap<String, String> = mutableMapOf(ContentType to JsonMediaType, Accept to JsonMediaType)"
def file = new File("./app/tmp/RestApi/src/main/kotlin/io/swagger/client/infrastructure/ApiClient.kt")
def newConfig = file.text.replace(token, value)
file.write newConfig
}
The result is a now changeable header :=
#JvmStatic
var defaultHeaders: MutableMap<String, String> = mutableMapOf(ContentType to JsonMediaType, Accept to JsonMediaType)

Akka-http & scribe for linkedin API: set/get cookie without session (scala)

I am using akka-http for bulding a REST API. (I am new to build REST web services).
I don't know how I can get and set cookie without using a session. This cookie must contain encrypt token access. I don't use Play or spray.
My code for the moment is:
lazy val signin = path("signin") {
get {
/* create the OAuthService object with a callback URL*/
val service = buildService()
/* get the request token*/
val requestToken = service.getRequestToken
/* create the cookie */
val jwtCookieEncrypted = tokenUtil.createLinkedinTokenSecret(requestToken)
val cookie = HttpCookie("jwtTokenCookie", jwtCookieEncrypted)
/* making the user validate our requestToken by redirecting him to the following URL*/
val authURL = service.getAuthorizationUrl(requestToken)
redirect(authURL, StatusCodes.TemporaryRedirect)
}
}
lazy val callback = path("callback") {
// extract cookie with the jwtTokenCookie name
cookie("jwtTokenCookie") { cookiePair =>
complete(s"The logged in user is '${cookiePair.name}'")
}
get {
parameters('code, 'state) { (code, state) => // must come from cookie and not request parameters
/* create the OAuthService object with a callback URL*/
val service = buildService()
/* get the request token*/
val requestToken = new Token(code, state)
if(state == tokenUtil.decryptLinkedinToken(requestToken.getSecret).getOrElse("")) "continue" else "throw error"
val verifier = new Verifier(state)
/* get the access token
(need to exchange requestToken and verifier for an accessToken which is the one used to sign requests)*/
val accessToken = service.getAccessToken(requestToken, verifier)
logger.debug(accessToken.getRawResponse)
/* sign request*/
val ResourceUrl = Settings.LinkedIn.ResourceUrl
val request = new OAuthRequest(Verb.GET, ResourceUrl)
service.signRequest(accessToken, request)
val response = request.send
if (response.getCode == StatusCodes.OK.intValue) complete(response.getBody)
else complete(int2StatusCode(response.getCode))
}
}
}
signin ~ callback
Check the akka doc. In your response you can include the header. In your case, maybe with redirect it´s not so simple. But you could complete the signing request returning a 308 Http code with the Location Header pointing to your oauth2 Auth server.
Is it better ?
path("signin") {
get {
val service = buildService()
val requestToken = service.getRequestToken
val authURL = service.getAuthorizationUrl(requestToken)
val requestTokenCrypted = tokenUtil.createLinkedinToken(requestToken)
val cookie = HttpCookie("abcde", requestTokenCrypted.getSecret)
setCookie(cookie) {
complete(HttpResponse(
status = StatusCodes.TemporaryRedirect,
headers = List(Location(authURL))
))
}
}
}

Specs2 - Mock objects inside method

I'm writting unit tests and i would like to know if it's possible to mock an object that is instanced inside the method that i'm testing.
Here is an example of a method that i would like to test:
def sendMessageToBroker(message:Message) = {
val soapBody = xmlBody("user", "pass", message.identifier,
message.to, message.message)
val response = new WebServiceUtil().doPost("uri", soapBody.toString(),
"text/xml; charset=utf-8", "action")
response
}
I was wondering if it's possible to do something like:
when call doPost, return new Response(200, 'Success')
Is it possible?
I've tried do it using spy() and mock, but no sucess:
val ws = new WebServiceUtil
val spiedObj = spy(ws)
spiedObj.doPost("uri", xml,
"text/xml; charset=utf-8",
"action") returns new Response(200, "Success")
val xx = messageService.sendMessageToBroker(new Message())
Any ideas on how can i do it?
You could write
val webService = mock[WebServiceUtil]
webService.doPost("uri", xml, "text/xml; charset=utf-8", "action") returns
new Response(200, "Success")
// pass the mock webservice as an argument
sendMessageToBroker(new Message, webService)
The important part is that you need to be able to pass a mock WebServiceUtil to your method being tested! There are many ways to do that. The simplest one is to pass the instance to a constructor method of our "class under test":
class MyClass(webService: WebServiceUtil) {
def sendMessageToBroker(message: Message) = {
// use the webservice
}
}
A more involved method would use Guice and the Inject annotation to pass the service (especially since you tagged the question as a play-framework-2.0 one). You will be interested in following this SOF question then.