From 8fe040db3cda07617a4bd09a77199b1b7b60a31b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Mili=C4=87?= Date: Thu, 26 Mar 2020 09:21:07 +0100 Subject: [PATCH] Add Card Attribute Documentation --- .../SwaggerDefinitionsJSON.scala | 18 +++ .../main/scala/code/api/util/ApiRole.scala | 11 +- .../scala/code/api/v4_0_0/APIMethods400.scala | 148 +++++++++++++++++- .../AttributeDocumentationCardTest.scala | 96 ++++++++++++ .../commons/model/enums/Enumerations.scala | 1 + 5 files changed, 270 insertions(+), 4 deletions(-) create mode 100644 obp-api/src/test/scala/code/api/v4_0_0/AttributeDocumentationCardTest.scala diff --git a/obp-api/src/main/scala/code/api/ResourceDocs1_4_0/SwaggerDefinitionsJSON.scala b/obp-api/src/main/scala/code/api/ResourceDocs1_4_0/SwaggerDefinitionsJSON.scala index 0252c5cd8..42d0025ad 100644 --- a/obp-api/src/main/scala/code/api/ResourceDocs1_4_0/SwaggerDefinitionsJSON.scala +++ b/obp-api/src/main/scala/code/api/ResourceDocs1_4_0/SwaggerDefinitionsJSON.scala @@ -3706,9 +3706,27 @@ object SwaggerDefinitionsJSON { val transactionAttributeDocumentationResponseJsonV400 = customerAttributeDocumentationResponseJsonV400.copy(category = AttributeCategory.Transaction.toString) + + val cardAttributeDocumentationJsonV400 = + customerAttributeDocumentationJsonV400.copy(category = AttributeCategory.Card.toString) + + val cardAttributeDocumentationResponseJsonV400 = + customerAttributeDocumentationResponseJsonV400.copy(category = AttributeCategory.Card.toString) val transactionAttributeDocumentationsResponseJsonV400 = AttributeDocumentationsResponseJsonV400( attributes = List(transactionAttributeDocumentationResponseJsonV400) + ) + val cardAttributeDocumentationsResponseJsonV400 = AttributeDocumentationsResponseJsonV400( + attributes = List(cardAttributeDocumentationResponseJsonV400) + ) + val accountAttributeDocumentationsResponseJsonV400 = AttributeDocumentationsResponseJsonV400( + attributes = List(accountAttributeDocumentationResponseJsonV400) + ) + val customerAttributeDocumentationsResponseJsonV400 = AttributeDocumentationsResponseJsonV400( + attributes = List(customerAttributeDocumentationResponseJsonV400) + ) + val productAttributeDocumentationsResponseJsonV400 = AttributeDocumentationsResponseJsonV400( + attributes = List(productAttributeDocumentationResponseJsonV400) ) //The common error or success format. diff --git a/obp-api/src/main/scala/code/api/util/ApiRole.scala b/obp-api/src/main/scala/code/api/util/ApiRole.scala index e97f71d65..ba434c63c 100644 --- a/obp-api/src/main/scala/code/api/util/ApiRole.scala +++ b/obp-api/src/main/scala/code/api/util/ApiRole.scala @@ -507,7 +507,16 @@ object ApiRole { lazy val canDeleteTransactionAttributeDocumentationAtOneBank = CanDeleteTransactionAttributeDocumentationAtOneBank() case class CanGetTransactionAttributeDocumentationAtOneBank(requiresBankId: Boolean = true) extends ApiRole - lazy val canGetTransactionAttributeDocumentationAtOneBank = CanGetTransactionAttributeDocumentationAtOneBank() + lazy val canGetTransactionAttributeDocumentationAtOneBank = CanGetTransactionAttributeDocumentationAtOneBank() + + case class CanGetCardAttributeDocumentationAtOneBank(requiresBankId: Boolean = true) extends ApiRole + lazy val canGetCardAttributeDocumentationAtOneBank = CanGetCardAttributeDocumentationAtOneBank() + + case class CanDeleteCardAttributeDocumentationAtOneBank(requiresBankId: Boolean = true) extends ApiRole + lazy val canDeleteCardAttributeDocumentationAtOneBank = CanDeleteCardAttributeDocumentationAtOneBank() + + case class CanCreateCardAttributeDocumentationAtOneBank(requiresBankId: Boolean = true) extends ApiRole + lazy val canCreateCardAttributeDocumentationAtOneBank = CanCreateCardAttributeDocumentationAtOneBank() private val dynamicApiRoles = new ConcurrentHashMap[String, ApiRole] diff --git a/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala b/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala index 6c1a5f492..ac3c0f439 100644 --- a/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala +++ b/obp-api/src/main/scala/code/api/v4_0_0/APIMethods400.scala @@ -2977,6 +2977,70 @@ trait APIMethods400 { + resourceDocs += ResourceDoc( + createOrUpdateCardAttributeDocumentation, + implementedInApiVersion, + nameOf(createOrUpdateCardAttributeDocumentation), + "PUT", + "/banks/BANK_ID/attribute-documentation/card", + "Create or Update Card Attribute Documentation", + s""" Create or Update Card Attribute Documentation + | + |The category field must be ${AttributeCategory.Card} + | + |The type field must be one of; ${AttributeType.DOUBLE}, ${AttributeType.STRING}, ${AttributeType.INTEGER} and ${AttributeType.DATE_WITH_DAY} + | + |${authenticationRequiredMessage(true)} + | + |""", + cardAttributeDocumentationJsonV400, + cardAttributeDocumentationResponseJsonV400, + List( + $UserNotLoggedIn, + $BankNotFound, + InvalidJsonFormat, + UnknownError + ), + Catalogs(notCore, notPSD2, notOBWG), + List(apiTagTransaction, apiTagNewStyle), + Some(List(canCreateCardAttributeDocumentationAtOneBank))) + + lazy val createOrUpdateCardAttributeDocumentation : OBPEndpoint = { + case "banks" :: BankId(bankId) :: "attribute-documentation" :: "card" :: Nil JsonPut json -> _=> { + cc => + val failMsg = s"$InvalidJsonFormat The Json body should be the $AttributeDocumentationJsonV400 " + for { + postedData <- NewStyle.function.tryons(failMsg, 400, cc.callContext) { + json.extract[AttributeDocumentationJsonV400] + } + failMsg = s"$InvalidJsonFormat The `Type` filed can only accept the following field: " + + s"${AttributeType.DOUBLE}(12.1234), ${AttributeType.STRING}(TAX_NUMBER), ${AttributeType.INTEGER} (123)and ${AttributeType.DATE_WITH_DAY}(2012-04-23)" + attributeType <- NewStyle.function.tryons(failMsg, 400, cc.callContext) { + AttributeType.withName(postedData.`type`) + } + failMsg = s"$InvalidJsonFormat The `Category` filed can only accept the following field: " + + s"${AttributeCategory.Card}" + category <- NewStyle.function.tryons(failMsg, 400, cc.callContext) { + AttributeCategory.withName(postedData.category) + } + (attributeDocumentation, callContext) <- createOrUpdateAttributeDocumentation( + bankId, + postedData.name, + category, + attributeType, + postedData.description, + postedData.alias, + postedData.is_active, + cc.callContext + ) + } yield { + (JSONFactory400.createAttributeDocumentationJson(attributeDocumentation), HttpCode.`201`(callContext)) + } + } + } + + + resourceDocs += ResourceDoc( deleteTransactionAttributeDocumentation, implementedInApiVersion, @@ -3130,6 +3194,45 @@ trait APIMethods400 { (Full(deleted), HttpCode.`200`(callContext)) } } + } + + + resourceDocs += ResourceDoc( + deleteCardAttributeDocumentation, + implementedInApiVersion, + nameOf(deleteCardAttributeDocumentation), + "DELETE", + "/banks/BANK_ID/attribute-documentation/ATTRIBUTE_DOCUMENTATION_ID/card", + "Delete Card Attribute Documentation", + s""" Delete Card Attribute Documentation by ATTRIBUTE_DOCUMENTATION_ID + | + |${authenticationRequiredMessage(true)} + | + |""", + emptyObjectJson, + emptyObjectJson, + List( + $UserNotLoggedIn, + $BankNotFound, + UnknownError + ), + Catalogs(notCore, notPSD2, notOBWG), + List(apiTagProduct, apiTagNewStyle), + Some(List(canDeleteCardAttributeDocumentationAtOneBank))) + + lazy val deleteCardAttributeDocumentation : OBPEndpoint = { + case "banks" :: BankId(bankId) :: "attribute-documentation" :: attributeDocumentationId :: "card" :: Nil JsonDelete _ => { + cc => + for { + (deleted, callContext) <- deleteAttributeDocumentation( + attributeDocumentationId, + AttributeCategory.withName(AttributeCategory.Card.toString), + cc.callContext + ) + } yield { + (Full(deleted), HttpCode.`200`(callContext)) + } + } } @@ -3146,7 +3249,7 @@ trait APIMethods400 { | |""", emptyObjectJson, - transactionAttributeDocumentationsResponseJsonV400, + productAttributeDocumentationsResponseJsonV400, List( $UserNotLoggedIn, $BankNotFound, @@ -3184,7 +3287,7 @@ trait APIMethods400 { | |""", emptyObjectJson, - transactionAttributeDocumentationsResponseJsonV400, + customerAttributeDocumentationsResponseJsonV400, List( $UserNotLoggedIn, $BankNotFound, @@ -3222,7 +3325,7 @@ trait APIMethods400 { | |""", emptyObjectJson, - transactionAttributeDocumentationsResponseJsonV400, + accountAttributeDocumentationsResponseJsonV400, List( $UserNotLoggedIn, $BankNotFound, @@ -3285,6 +3388,45 @@ trait APIMethods400 { } + + resourceDocs += ResourceDoc( + getCardAttributeDocumentation, + implementedInApiVersion, + nameOf(getCardAttributeDocumentation), + "GET", + "/banks/BANK_ID/attribute-documentation/card", + "Get Card Attribute Documentation", + s""" Get Card Attribute Documentation + | + |${authenticationRequiredMessage(true)} + | + |""", + emptyObjectJson, + cardAttributeDocumentationsResponseJsonV400, + List( + $UserNotLoggedIn, + $BankNotFound, + UnknownError + ), + Catalogs(notCore, notPSD2, notOBWG), + List(apiTagTransaction, apiTagNewStyle), + Some(List(canGetCardAttributeDocumentationAtOneBank))) + + lazy val getCardAttributeDocumentation : OBPEndpoint = { + case "banks" :: BankId(bankId) :: "attribute-documentation" :: "card" :: Nil JsonGet _ => { + cc => + for { + (attributeDocumentations, callContext) <- getAttributeDocumentation( + AttributeCategory.withName(AttributeCategory.Card.toString), + cc.callContext + ) + } yield { + (JSONFactory400.createAttributeDocumentationsJson(attributeDocumentations), HttpCode.`200`(callContext)) + } + } + } + + } diff --git a/obp-api/src/test/scala/code/api/v4_0_0/AttributeDocumentationCardTest.scala b/obp-api/src/test/scala/code/api/v4_0_0/AttributeDocumentationCardTest.scala new file mode 100644 index 000000000..5eb4046f2 --- /dev/null +++ b/obp-api/src/test/scala/code/api/v4_0_0/AttributeDocumentationCardTest.scala @@ -0,0 +1,96 @@ +package code.api.v4_0_0 + +import code.api.ResourceDocs1_4_0.SwaggerDefinitionsJSON +import code.api.util.APIUtil.OAuth._ +import code.api.util.ErrorMessages.{UserHasMissingRoles, UserNotLoggedIn} +import code.api.v4_0_0.OBPAPI4_0_0.Implementations4_0_0 +import com.github.dwickern.macros.NameOf.nameOf +import com.openbankproject.commons.model.ErrorMessage +import com.openbankproject.commons.util.ApiVersion +import net.liftweb.json.Serialization.write +import org.scalatest.Tag + +class AttributeDocumentationCardTest extends V400ServerSetup { + /** + * Test tags + * Example: To run tests with tag "getPermissions": + * mvn test -D tagsToInclude + * + * This is made possible by the scalatest maven plugin + */ + object VersionOfApi extends Tag(ApiVersion.v4_0_0.toString) + object ApiEndpoint1 extends Tag(nameOf(Implementations4_0_0.createOrUpdateCardAttributeDocumentation)) + object ApiEndpoint2 extends Tag(nameOf(Implementations4_0_0.getCardAttributeDocumentation)) + object ApiEndpoint3 extends Tag(nameOf(Implementations4_0_0.deleteCardAttributeDocumentation)) + + + lazy val bankId = randomBankId + lazy val putJson = SwaggerDefinitionsJSON.cardAttributeDocumentationJsonV400 + + feature(s"test $ApiEndpoint1 version $VersionOfApi - Unauthorized access") { + scenario("We will call the endpoint without user credentials", ApiEndpoint1, VersionOfApi) { + When("We make a request v4.0.0") + val request400 = (v4_0_0_Request / "banks" / bankId / "attribute-documentation" / "card").PUT + val response400 = makePutRequest(request400, write(putJson)) + Then("We should get a 400") + response400.code should equal(400) + response400.body.extract[ErrorMessage].message should equal(UserNotLoggedIn) + } + } + feature(s"test $ApiEndpoint2 version $VersionOfApi - Unauthorized access") { + scenario("We will call the endpoint without user credentials", ApiEndpoint2, VersionOfApi) { + When("We make a request v4.0.0") + val request400 = (v4_0_0_Request / "banks" / bankId / "attribute-documentation" / "card").GET + val response400 = makeGetRequest(request400) + Then("We should get a 400") + response400.code should equal(400) + response400.body.extract[ErrorMessage].message should equal(UserNotLoggedIn) + } + } + feature(s"test $ApiEndpoint3 version $VersionOfApi - Unauthorized access") { + scenario("We will call the endpoint without user credentials", ApiEndpoint3, VersionOfApi) { + When("We make a request v4.0.0") + val request400 = (v4_0_0_Request / "banks" / bankId / "attribute-documentation" + / "ATTRIBUTE_DOCUMENTATION_ID" / "card").DELETE + val response400 = makeDeleteRequest(request400) + Then("We should get a 400") + response400.code should equal(400) + response400.body.extract[ErrorMessage].message should equal(UserNotLoggedIn) + } + } + + feature(s"test $ApiEndpoint1 version $VersionOfApi - authorized access- missing role") { + scenario("We will call the endpoint with user credentials", ApiEndpoint1, VersionOfApi) { + When("We make a request v4.0.0") + val request400 = (v4_0_0_Request / "banks" / bankId / "attribute-documentation" / "card").PUT <@ (user1) + val response400 = makePutRequest(request400, write(putJson)) + Then("We should get a 403") + response400.code should equal(403) + response400.body.extract[ErrorMessage].message.toString contains (UserHasMissingRoles) should be (true) + } + } + feature(s"test $ApiEndpoint2 version $VersionOfApi - authorized access- missing role") { + scenario("We will call the endpoint with user credentials", ApiEndpoint2, VersionOfApi) { + When("We make a request v4.0.0") + val request400 = (v4_0_0_Request / "banks" / bankId / "attribute-documentation" / "card").GET <@ (user1) + val response400 = makeGetRequest(request400) + Then("We should get a 403") + response400.code should equal(403) + response400.body.extract[ErrorMessage].message.toString contains (UserHasMissingRoles) should be (true) + } + } + feature(s"test $ApiEndpoint3 version $VersionOfApi - authorized access- missing role") { + scenario("We will call the endpoint with user credentials", ApiEndpoint3, VersionOfApi) { + When("We make a request v4.0.0") + val request400 = (v4_0_0_Request / "banks" / bankId / "attribute-documentation" + / "ATTRIBUTE_DOCUMENTATION_ID" / "card").DELETE <@ (user1) + val response400 = makeDeleteRequest(request400) + Then("We should get a 403") + response400.code should equal(403) + response400.body.extract[ErrorMessage].message.toString contains (UserHasMissingRoles) should be (true) + } + } + + + +} diff --git a/obp-commons/src/main/scala/com/openbankproject/commons/model/enums/Enumerations.scala b/obp-commons/src/main/scala/com/openbankproject/commons/model/enums/Enumerations.scala index bc2f7fc13..8d1fe6306 100644 --- a/obp-commons/src/main/scala/com/openbankproject/commons/model/enums/Enumerations.scala +++ b/obp-commons/src/main/scala/com/openbankproject/commons/model/enums/Enumerations.scala @@ -130,4 +130,5 @@ object AttributeCategory extends OBPEnumeration[AttributeCategory]{ object Product extends Value object Account extends Value object Transaction extends Value + object Card extends Value } \ No newline at end of file