feature/Add endpoint to list system views

This commit is contained in:
Marko Milić 2022-10-21 09:35:06 +02:00
parent 73e6d29f9c
commit 95b704b9f7
9 changed files with 107 additions and 1 deletions

View File

@ -3005,6 +3005,11 @@ object SwaggerDefinitionsJSON {
views = List(viewJsonV500)
)
val viewIdJsonV500 = ViewIdJsonV500(id = "owner")
val viewIdsJsonV500 = ViewsIdsJsonV500(
views = List(viewIdJsonV500)
)
val fXRateJSON = FXRateJsonV220(
bank_id = bankIdExample.value,
from_currency_code = "EUR",

View File

@ -654,6 +654,9 @@ object NewStyle extends MdcLoggable{
Views.views.vend.systemViewFuture(viewId) map {
unboxFullOrFail(_, callContext, s"$SystemViewNotFound. Current ViewId is $viewId")
}
}
def systemViews(): Future[List[View]] = {
Views.views.vend.getSystemViews()
}
def grantAccessToCustomView(view : View, user: User, callContext: Option[CallContext]) : Future[View] = {
view.isSystem match {

View File

@ -17,7 +17,7 @@ import code.api.v3_0_0.JSONFactory300
import code.api.v3_1_0._
import code.api.v4_0_0.JSONFactory400.createCustomersMinimalJson
import code.api.v4_0_0.{JSONFactory400, PutProductJsonV400}
import code.api.v5_0_0.JSONFactory500.{createPhysicalCardJson, createViewJsonV500, createViewsJsonV500}
import code.api.v5_0_0.JSONFactory500.{createPhysicalCardJson, createViewJsonV500, createViewsJsonV500, createViewsIdsJsonV500}
import code.bankconnectors.Connector
import code.consent.{ConsentRequests, Consents}
import code.entitlement.Entitlement
@ -1659,6 +1659,40 @@ trait APIMethods500 {
}
}
}
staticResourceDocs += ResourceDoc(
getSystemViewsIds,
implementedInApiVersion,
nameOf(getSystemViewsIds),
"GET",
"/system-views-ids",
"Get Ids of System Views",
s"""Get Ids of System Views
|
|${authenticationRequiredMessage(true)}
|
""".stripMargin,
emptyObjectJson,
viewIdsJsonV500,
List(
$UserNotLoggedIn,
$BankNotFound,
UnknownError
),
List(apiTagSystemView, apiTagNewStyle),
Some(List(canGetSystemView))
)
lazy val getSystemViewsIds: OBPEndpoint = {
case "system-views-ids" :: Nil JsonGet _ => {
cc =>
for {
views <- NewStyle.function.systemViews()
} yield {
(createViewsIdsJsonV500(views), HttpCode.`200`(cc.callContext))
}
}
}
staticResourceDocs += ResourceDoc(

View File

@ -418,6 +418,9 @@ case class UpdateViewJsonV500(
}
case class ViewsJsonV500(views : List[ViewJsonV500])
case class ViewIdJsonV500(id: String)
case class ViewsIdsJsonV500(views : List[ViewIdJsonV500])
case class ViewJsonV500(
val id: String,
val short_name: String,
@ -836,6 +839,11 @@ object JSONFactory500 {
def createViewsJsonV500(views : List[View]) : ViewsJsonV500 = {
ViewsJsonV500(views.map(createViewJsonV500))
}
def createViewsIdsJsonV500(views : List[View]) : ViewsIdsJsonV500 = {
ViewsIdsJsonV500(views.map(i => ViewIdJsonV500(i.viewId.value)))
}
}

View File

@ -69,6 +69,10 @@ object RemotedataViews extends ObpActorInit with Views {
def systemViewFuture(viewId : ViewId) : Future[Box[View]] =
(actor ? cc.systemViewFuture(viewId)).mapTo[Box[View]]
def getSystemViews() : Future[List[View]] =
(actor ? cc.getSystemViews()).mapTo[List[View]]
def createView(bankAccountId: BankIdAccountId, view: CreateViewJson): Box[View] = getValueFromFuture(
(actor ? cc.createView(bankAccountId, view)).mapTo[Box[View]]
)

View File

@ -64,6 +64,10 @@ class RemotedataViewsActor extends Actor with ObpActorHelper with MdcLoggable {
case cc.systemView(viewId: ViewId) =>
logger.debug("view(" + viewId + ")")
sender ! (mapper.systemView(viewId))
case cc.getSystemViews() =>
logger.debug("getSystemViews()")
sender ! (mapper.getSystemViews())
case cc.customViewFuture(viewId: ViewId, bankAccountId: BankIdAccountId) =>
logger.debug("customViewFuture(" + viewId +", "+ bankAccountId + ")")

View File

@ -340,6 +340,15 @@ object MapperViews extends Views with MdcLoggable {
def systemView(viewId : ViewId) : Box[View] = {
ViewDefinition.findSystemView(viewId.value)
}
def getSystemViews() : Future[List[View]] = {
Future {
ViewDefinition.findAll(
NullRef(ViewDefinition.bank_id),
NullRef(ViewDefinition.account_id),
By(ViewDefinition.isSystem_, true)
)
}
}
def systemViewFuture(viewId : ViewId) : Future[Box[View]] = {
Future {
systemView(viewId)

View File

@ -55,6 +55,7 @@ trait Views {
def systemView(viewId : ViewId) : Box[View]
def customViewFuture(viewId : ViewId, bankAccountId: BankIdAccountId) : Future[Box[View]]
def systemViewFuture(viewId : ViewId) : Future[Box[View]]
def getSystemViews(): Future[List[View]]
//always return a view id String, not error here.
def getMetadataViewId(bankAccountId: BankIdAccountId, viewId : ViewId) = Views.views.vend.customView(viewId, bankAccountId).map(_.metadataView).openOr(viewId.value)
@ -154,6 +155,7 @@ class RemotedataViewsCaseClasses {
def apply(viewId: ViewId, bankAccountId: BankIdAccountId): Box[View] = this (viewId, bankAccountId)
}
case class systemView(viewId : ViewId)
case class getSystemViews()
case class customViewFuture(viewId : ViewId, bankAccountId: BankIdAccountId)
case class systemViewFuture(viewId : ViewId)
case class getOrCreateAccountView(account: BankIdAccountId, viewName: String)

View File

@ -67,6 +67,7 @@ class SystemViewsTests extends V500ServerSetup {
object ApiEndpoint2 extends Tag(nameOf(Implementations5_0_0.createSystemView))
object ApiEndpoint3 extends Tag(nameOf(Implementations5_0_0.updateSystemView))
object ApiEndpoint4 extends Tag(nameOf(Implementations5_0_0.deleteSystemView))
object ApiEndpoint5 extends Tag(nameOf(Implementations5_0_0.getSystemViewsIds))
// Custom view, name starts from `_`
// System view, owner
@ -79,6 +80,10 @@ class SystemViewsTests extends V500ServerSetup {
def getSystemView(viewId : String, consumerAndToken: Option[(Consumer, Token)]): APIResponse = {
val request = v5_0_0_Request / "system-views" / viewId <@(consumerAndToken)
makeGetRequest(request)
}
def getSystemViewsIds(consumerAndToken: Option[(Consumer, Token)]): APIResponse = {
val request = v5_0_0_Request / "system-views-ids" <@(consumerAndToken)
makeGetRequest(request)
}
def postSystemView(view: CreateViewJson, consumerAndToken: Option[(Consumer, Token)]): APIResponse = {
val request = (v5_0_0_Request / "system-views").POST <@(consumerAndToken)
@ -274,4 +279,36 @@ class SystemViewsTests extends V500ServerSetup {
response400.code should equal(200)
}
}
feature(s"test $ApiEndpoint5 version $VersionOfApi - Unauthorized access") {
scenario("We will call the endpoint without user credentials", ApiEndpoint1, VersionOfApi) {
When(s"We make a request $ApiEndpoint5")
val response400 = getSystemViewsIds(None)
Then("We should get a 401")
response400.code should equal(401)
response400.body.extract[ErrorMessage].message should equal(UserNotLoggedIn)
}
}
feature(s"test $ApiEndpoint5 version $VersionOfApi - Authorized access") {
scenario("We will call the endpoint without user credentials", ApiEndpoint1, VersionOfApi) {
When(s"We make a request $ApiEndpoint2")
val response400 = getSystemViewsIds(user1)
Then("We should get a 403")
response400.code should equal(403)
response400.body.extract[ErrorMessage].message should equal(UserHasMissingRoles + CanGetSystemView)
}
}
feature(s"test $ApiEndpoint5 version $VersionOfApi - Authorized access with proper Role") {
scenario("We will call the endpoint without user credentials", ApiEndpoint1, VersionOfApi) {
When(s"We make a request $ApiEndpoint2")
Entitlement.entitlement.vend.addEntitlement("", resourceUser1.userId, CanGetSystemView.toString)
val response400 = getSystemViewsIds(user1)
Then("We should get a 200")
response400.code should equal(200)
response400.body.extract[ViewsIdsJsonV500]
}
}
}