- package com.knoldus.leader_board.application
- import akka.http.javadsl.server.AuthorizationFailedRejection
- import akka.http.scaladsl.model.StatusCodes
- import akka.http.scaladsl.model.headers.{Authorization, OAuth2BearerToken}
- import akka.http.scaladsl.testkit.ScalatestRouteTest
- import com.knoldus.leader_board.{ContributionStatus, ContributionType, Contributions_constants, KnolderContribution}
- import net.liftweb.json.{DefaultFormats, compactRender}
- import org.mockito.MockitoSugar
- import org.scalatest.matchers.should.Matchers
- import org.scalatest.wordspec.AnyWordSpecLike
- import com.knoldus.leader_board.business.{Contributions, ContributionsImpl}
- import com.knoldus.leader_board.utils.{Error, StandardResponseForCaseClass, StandardResponseForListCaseClass}
- import net.liftweb.json.Extraction.decompose
- import org.keycloak.admin.client.Keycloak
- import org.keycloak.common.util.UriUtils
- class ContributionAPIImplSpec extends AnyWordSpecLike with MockitoSugar with Matchers with ScalatestRouteTest {
- implicit val formats: DefaultFormats.type = DefaultFormats
- val mockContribution: Contributions = mock[ContributionsImpl]
- val contributionApi: ContributionAPI = new ContributionAPIImpl(mockContribution)
- val JWTHelper = JWTTokenHelper
- val JWTAdmin = JWTTokenHelper.createJwtTokenWithRole("[email protected]", "admin")
- val JWTEmployee = JWTTokenHelper.createJwtTokenWithRole("[email protected]", "employee")
- val JWTUnauthorized = JWTTokenHelper.createJwtTokenWithRole("[email protected]", "abc")
- val authServer: String = UriUtils.getOrigin("http://auth.knoldus.com") + "/auth"
- val keycloakAdmin: Keycloak = Keycloak.getInstance(authServer, "knoldus", "testadmin",
- "testadmin", "leaderboard-ui")
- val adminOauthToken = keycloakAdmin.tokenManager().getAccessTokenString
- val adminOriginHeader = Authorization(OAuth2BearerToken(adminOauthToken))
- val keycloakEmployee: Keycloak = Keycloak.getInstance(authServer, "knoldus", "testemployee",
- "testemployee", "leaderboard-ui")
- val employeeOauthToken = keycloakEmployee.tokenManager().getAccessTokenString
- val employeeOriginHeader = Authorization(OAuth2BearerToken(employeeOauthToken))
- val studioId = 9
- "Get all pending contributions route" should {
- val contributionDetails: KnolderContribution = KnolderContribution(
- contributionId = 233,
- contributorName = "Average Joe",
- contributorEmail = "[email protected]",
- contributionType = ContributionType.OPEN_SOURCE,
- title = "TEST",
- contributionDate = "2021-12-26 00:00:00.0",
- technologyDetails = "Test description",
- urlDetails = "http://test-contribution",
- status = ContributionStatus.PENDING
- )
- "return an unauthorized response when neither a admin or employee user tries to access the endpoint" in {
- Get(uri = "/getAllPendingContributions?access_token=" + JWTUnauthorized + "&studioId=9") ~> contributionApi.getAllPendingContributionsRoute ~> check {
- rejection.isInstanceOf[AuthorizationFailedRejection]
- }
- }
- // "return response with 204 no content status code since there are no rewards in the database to fetch" in {
- //
- // when(mockContribution.getAllPendingContributions(studioId)).thenReturn(StandardResponseForListCaseClass(
- // resource = Some("getAllPendingContributions"),
- // status = true,
- // errors = None,
- // data = Some(List())
- // ))
- //
- // Get(uri = "/getAllPendingContributions?access_token=" + JWTAdmin) ~> adminOriginHeader ~> contributionApi.getAllPendingContributionsRoute ~> check {
- // status shouldBe StatusCodes.NoContent
- // }
- //
- // }
- "return response with error id CON_001 and 500 internal server error status code since a sql exception was caught" in {
- when(mockContribution.getAllPendingContributions(10)).thenReturn(StandardResponseForListCaseClass(
- resource = Some("/getAllPendingContributions"),
- status = false,
- errors = Some(List(Error(
- id = Contributions_constants.CON_001,
- message = "Some sql exception caught"
- ))),
- data = None
- ))
- Get(uri = "/getAllPendingContributions?access_token=" + JWTAdmin + "&studioId=10") ~> adminOriginHeader ~> contributionApi.getAllPendingContributionsRoute ~> check {
- status shouldBe StatusCodes.InternalServerError
- responseAs[String] shouldBe compactRender(decompose(StandardResponseForListCaseClass(
- resource = Some("/getAllPendingContributions"),
- status = false,
- errors = Some(List(Error(
- id = Contributions_constants.CON_001,
- message = "Some sql exception caught"
- ))),
- data = None
- )))
- }
- }
- "return response with all the pending contributions from the database" in {
- when(mockContribution.getAllPendingContributions(studioId)).thenReturn {
- val result: List[KnolderContribution] = List(
- KnolderContribution(
- contributionId = 233,
- contributorName = "Average Joe",
- contributorEmail = "[email protected]",
- contributionType = ContributionType.OPEN_SOURCE,
- title = "TEST",
- contributionDate = "2021-12-26 00:00:00.0",
- technologyDetails = "Test description",
- urlDetails = "http://test-contribution",
- status = ContributionStatus.PENDING
- ),
- KnolderContribution(
- contributionId = 105,
- contributorName = "John murphy",
- contributorEmail = "[email protected]",
- contributionType = ContributionType.OPEN_SOURCE,
- title = "TEST",
- contributionDate = "2021-12-26 00:00:00.0",
- technologyDetails = "Test description",
- urlDetails = "http://test-contribution",
- status = ContributionStatus.PENDING
- )
- )
- StandardResponseForListCaseClass(
- resource = Some("/getAllPendingContributions"),
- status = true,
- errors = None,
- data = Some(result)
- )
- }
- Get(uri = "/getAllPendingContributions?access_token=" + JWTAdmin + "&studioId=9") ~> adminOriginHeader ~>
- contributionApi.getAllPendingContributionsRoute ~> check {
- responseAs[String] shouldBe compactRender(decompose(StandardResponseForListCaseClass(
- resource = Some("/getAllPendingContributions"),
- status = true,
- errors = None,
- data = Some(List(
- KnolderContribution(
- contributionId = 233,
- contributorName = "Average Joe",
- contributorEmail = "[email protected]",
- contributionType = ContributionType.OPEN_SOURCE,
- title = "TEST",
- contributionDate = "2021-12-26 00:00:00.0",
- technologyDetails = "Test description",
- urlDetails = "http://test-contribution",
- status = ContributionStatus.PENDING
- ),
- KnolderContribution(
- contributionId = 105,
- contributorName = "John murphy",
- contributorEmail = "[email protected]",
- contributionType = ContributionType.OPEN_SOURCE,
- title = "TEST",
- contributionDate = "2021-12-26 00:00:00.0",
- technologyDetails = "Test description",
- urlDetails = "http://test-contribution",
- status = ContributionStatus.PENDING
- )
- ))
- )))
- }
- }
- }
- }
[text] dude
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new:
File Description
- dude
- Paste Code
- 20 Jan-2022
- 7.6 Kb
You can Share it:
Latest Code Pastes