Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2021 Typelevel
*
* 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 feral.lambda
package events

import io.circe.Decoder
import io.circe.Json
import org.typelevel.ci.CIString

import codecs._

sealed abstract class AppSyncRequestContext {
def apiId: String
def accountId: String
def requestId: String
def queryString: String
def operationName: String
def variables: Map[String, Json]
}

object AppSyncRequestContext {

def apply(
apiId: String,
accountId: String,
requestId: String,
queryString: String,
operationName: String,
variables: Map[String, Json]
): AppSyncRequestContext =
new Impl(
apiId,
accountId,
requestId,
queryString,
operationName,
variables
)

implicit def decoder: Decoder[AppSyncRequestContext] = Decoder.forProduct6(
"apiId",
"accountId",
"requestId",
"queryString",
"operationName",
"variables"
)(AppSyncRequestContext.apply)

private final case class Impl(
apiId: String,
accountId: String,
requestId: String,
queryString: String,
operationName: String,
variables: Map[String, Json]
) extends AppSyncRequestContext {
override def productPrefix = "AppSyncRequestContext"
}
}

sealed abstract class AppSyncLambdaAuthorizerEvent {
def authorizationToken: String
def requestContext: AppSyncRequestContext
def requestHeaders: Map[CIString, String]
}

object AppSyncLambdaAuthorizerEvent {
def apply(
authorizationToken: String,
requestContext: AppSyncRequestContext,
requestHeaders: Map[CIString, String]
): AppSyncLambdaAuthorizerEvent =
new Impl(
authorizationToken: String,
requestContext: AppSyncRequestContext,
requestHeaders: Map[CIString, String]
)

implicit def decoder: Decoder[AppSyncLambdaAuthorizerEvent] = Decoder.forProduct3(
"authorizationToken",
"requestContext",
"requestHeaders"
)(AppSyncLambdaAuthorizerEvent.apply)

private final case class Impl(
authorizationToken: String,
requestContext: AppSyncRequestContext,
requestHeaders: Map[CIString, String]
) extends AppSyncLambdaAuthorizerEvent {
override def productPrefix = "AppSyncLambdaAuthorizerEvent"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2021 Typelevel
*
* 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 feral.lambda.events

import io.circe.literal._
import munit.FunSuite
import org.typelevel.ci.CIString

class AppSyncLambdaAuthorizerEventSuite extends FunSuite {

test("decoder") {
import AppSyncLambdaAuthorizerEventSuite._

val parsed_event = event.as[AppSyncLambdaAuthorizerEvent].toTry.get

assertEquals(parsed_event.authorizationToken, "ExampleAUTHtoken123123123")
assertEquals(parsed_event.requestContext.accountId, "111122223333")
assertEquals(parsed_event.requestContext.apiId, "aaaaaa123123123example123")
assertEquals(parsed_event.requestContext.requestId, "f4081827-1111-4444-5555-5cf4695f339f")
assertEquals(parsed_event.requestHeaders(CIString("header")), "value")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, one last comment (it applies to all of your PRs): instead of checking each field individually, could we construct an instance of the object and assert its equality like in the existing tests? I think this is a preferable approach because we can be confident we didn't forget to test any fields, etc.

assertEquals(event.as[S3Event].toTry.get, result)

}

}

object AppSyncLambdaAuthorizerEventSuite {
def event = json"""
{
"authorizationToken": "ExampleAUTHtoken123123123",
"requestContext": {
"apiId": "aaaaaa123123123example123",
"accountId": "111122223333",
"requestId": "f4081827-1111-4444-5555-5cf4695f339f",
"queryString": "mutation CreateEvent {...}\n\nquery MyQuery {...}\n",
"operationName": "MyQuery",
"variables": {}
},
"requestHeaders": {
"header": "value"
}
}
"""
}
Loading