[text] dude

Viewer

  1. package com.knoldus.leader_board.application
  2.  
  3. import akka.http.javadsl.server.AuthorizationFailedRejection
  4. import akka.http.scaladsl.model.StatusCodes
  5. import akka.http.scaladsl.model.headers.{Authorization, OAuth2BearerToken}
  6. import akka.http.scaladsl.testkit.ScalatestRouteTest
  7. import com.knoldus.leader_board.{ContributionStatus, ContributionType, Contributions_constants, KnolderContribution}
  8. import net.liftweb.json.{DefaultFormats, compactRender}
  9. import org.mockito.MockitoSugar
  10. import org.scalatest.matchers.should.Matchers
  11. import org.scalatest.wordspec.AnyWordSpecLike
  12. import com.knoldus.leader_board.business.{Contributions, ContributionsImpl}
  13. import com.knoldus.leader_board.utils.{Error, StandardResponseForCaseClass, StandardResponseForListCaseClass}
  14. import net.liftweb.json.Extraction.decompose
  15. import org.keycloak.admin.client.Keycloak
  16. import org.keycloak.common.util.UriUtils
  17.  
  18. class ContributionAPIImplSpec extends AnyWordSpecLike with MockitoSugar with Matchers with ScalatestRouteTest {
  19.  
  20.   implicit val formats: DefaultFormats.type = DefaultFormats
  21.   val mockContribution: Contributions = mock[ContributionsImpl]
  22.   val contributionApi: ContributionAPI = new ContributionAPIImpl(mockContribution)
  23.  
  24.   val JWTHelper = JWTTokenHelper
  25.   val JWTAdmin = JWTTokenHelper.createJwtTokenWithRole("[email protected]", "admin")
  26.   val JWTEmployee = JWTTokenHelper.createJwtTokenWithRole("[email protected]", "employee")
  27.   val JWTUnauthorized = JWTTokenHelper.createJwtTokenWithRole("[email protected]", "abc")
  28.  
  29.   val authServer: String = UriUtils.getOrigin("http://auth.knoldus.com") + "/auth"
  30.   val keycloakAdmin: Keycloak = Keycloak.getInstance(authServer, "knoldus", "testadmin",
  31.     "testadmin", "leaderboard-ui")
  32.   val adminOauthToken = keycloakAdmin.tokenManager().getAccessTokenString
  33.   val adminOriginHeader = Authorization(OAuth2BearerToken(adminOauthToken))
  34.  
  35.   val keycloakEmployee: Keycloak = Keycloak.getInstance(authServer, "knoldus", "testemployee",
  36.     "testemployee", "leaderboard-ui")
  37.   val employeeOauthToken = keycloakEmployee.tokenManager().getAccessTokenString
  38.   val employeeOriginHeader = Authorization(OAuth2BearerToken(employeeOauthToken))
  39.  
  40.   val studioId = 9
  41.  
  42.  
  43.   "Get all pending contributions route" should {
  44.  
  45.     val contributionDetails: KnolderContribution = KnolderContribution(
  46.       contributionId = 233,
  47.       contributorName = "Average Joe",
  48.       contributorEmail = "[email protected]",
  49.       contributionType = ContributionType.OPEN_SOURCE,
  50.       title = "TEST",
  51.       contributionDate = "2021-12-26 00:00:00.0",
  52.       technologyDetails = "Test description",
  53.       urlDetails = "http://test-contribution",
  54.       status = ContributionStatus.PENDING
  55.     )
  56.  
  57.  
  58.     "return an unauthorized response when neither a admin or employee user tries to access the endpoint" in {
  59.  
  60.       Get(uri = "/getAllPendingContributions?access_token=" + JWTUnauthorized + "&studioId=9") ~> contributionApi.getAllPendingContributionsRoute ~> check {
  61.         rejection.isInstanceOf[AuthorizationFailedRejection]
  62.       }
  63.  
  64.     }
  65.  
  66. //    "return response with 204 no content status code since there are no rewards in the database to fetch" in {
  67. //
  68. //      when(mockContribution.getAllPendingContributions(studioId)).thenReturn(StandardResponseForListCaseClass(
  69. //        resource = Some("getAllPendingContributions"),
  70. //        status = true,
  71. //        errors = None,
  72. //        data = Some(List())
  73. //      ))
  74. //
  75. //      Get(uri = "/getAllPendingContributions?access_token=" + JWTAdmin) ~> adminOriginHeader ~> contributionApi.getAllPendingContributionsRoute ~> check {
  76. //        status shouldBe StatusCodes.NoContent
  77. //      }
  78. //
  79. //    }
  80.  
  81.     "return response with error id CON_001 and 500 internal server error status code since a sql exception was caught" in {
  82.  
  83.       when(mockContribution.getAllPendingContributions(10)).thenReturn(StandardResponseForListCaseClass(
  84.         resource = Some("/getAllPendingContributions"),
  85.         status = false,
  86.         errors = Some(List(Error(
  87.           id = Contributions_constants.CON_001,
  88.           message = "Some sql exception caught"
  89.         ))),
  90.         data = None
  91.       ))
  92.  
  93.       Get(uri = "/getAllPendingContributions?access_token=" + JWTAdmin + "&studioId=10") ~> adminOriginHeader ~> contributionApi.getAllPendingContributionsRoute ~> check {
  94.         status shouldBe StatusCodes.InternalServerError
  95.         responseAs[String] shouldBe compactRender(decompose(StandardResponseForListCaseClass(
  96.           resource = Some("/getAllPendingContributions"),
  97.           status = false,
  98.           errors = Some(List(Error(
  99.             id = Contributions_constants.CON_001,
  100.             message = "Some sql exception caught"
  101.           ))),
  102.           data = None
  103.         )))
  104.       }
  105.     }
  106.  
  107.     "return response with all the pending contributions from the database"  in {
  108.       when(mockContribution.getAllPendingContributions(studioId)).thenReturn {
  109.         val result: List[KnolderContribution] = List(
  110.           KnolderContribution(
  111.             contributionId = 233,
  112.             contributorName = "Average Joe",
  113.             contributorEmail = "[email protected]",
  114.             contributionType = ContributionType.OPEN_SOURCE,
  115.             title = "TEST",
  116.             contributionDate = "2021-12-26 00:00:00.0",
  117.             technologyDetails = "Test description",
  118.             urlDetails = "http://test-contribution",
  119.             status = ContributionStatus.PENDING
  120.           ),
  121.           KnolderContribution(
  122.             contributionId = 105,
  123.             contributorName = "John murphy",
  124.             contributorEmail = "[email protected]",
  125.             contributionType = ContributionType.OPEN_SOURCE,
  126.             title = "TEST",
  127.             contributionDate = "2021-12-26 00:00:00.0",
  128.             technologyDetails = "Test description",
  129.             urlDetails = "http://test-contribution",
  130.             status = ContributionStatus.PENDING
  131.           )
  132.         )
  133.  
  134.         StandardResponseForListCaseClass(
  135.           resource = Some("/getAllPendingContributions"),
  136.           status = true,
  137.           errors = None,
  138.           data = Some(result)
  139.         )
  140.       }
  141.  
  142.       Get(uri = "/getAllPendingContributions?access_token=" + JWTAdmin + "&studioId=9") ~> adminOriginHeader ~>
  143.         contributionApi.getAllPendingContributionsRoute ~> check {
  144.         responseAs[String] shouldBe compactRender(decompose(StandardResponseForListCaseClass(
  145.           resource = Some("/getAllPendingContributions"),
  146.           status = true,
  147.           errors = None,
  148.           data = Some(List(
  149.             KnolderContribution(
  150.               contributionId = 233,
  151.               contributorName = "Average Joe",
  152.               contributorEmail = "[email protected]",
  153.               contributionType = ContributionType.OPEN_SOURCE,
  154.               title = "TEST",
  155.               contributionDate = "2021-12-26 00:00:00.0",
  156.               technologyDetails = "Test description",
  157.               urlDetails = "http://test-contribution",
  158.               status = ContributionStatus.PENDING
  159.             ),
  160.             KnolderContribution(
  161.               contributionId = 105,
  162.               contributorName = "John murphy",
  163.               contributorEmail = "[email protected]",
  164.               contributionType = ContributionType.OPEN_SOURCE,
  165.               title = "TEST",
  166.               contributionDate = "2021-12-26 00:00:00.0",
  167.               technologyDetails = "Test description",
  168.               urlDetails = "http://test-contribution",
  169.               status = ContributionStatus.PENDING
  170.             )
  171.           ))
  172.         )))
  173.       }
  174.  
  175.     }
  176.  
  177.  
  178.  
  179.  
  180.  
  181.   }
  182. }
  183.  
  184.  

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: