<commit_before>/**
 *  Copyright (C) 2012 Orbeon, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the
 *  GNU Lesser General Public License as published by the Free Software Foundation; either version
 *  2.1 of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Lesser General Public License for more details.
 *
 *  The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.controller

import org.orbeon.oxf.http.Headers._
import org.junit.Test
import org.mockito.Mockito
import org.orbeon.oxf.externalcontext.{ExternalContext, WebAppContext}

import collection.JavaConverters._
import org.scalatestplus.junit.AssertionsForJUnit
import org.scalatestplus.mockito.MockitoSugar

class AuthorizerTest extends AssertionsForJUnit with MockitoSugar {

  import Authorizer._

  @Test def testAuthorizedWithToken(): Unit = {

    val appAttributes     = collection.mutable.Map[String, AnyRef]()
    val requestAttributes = collection.mutable.Map[String, Array[String]]()

    val ec = mock[ExternalContext]
    val webAppContext = mock[WebAppContext]
    val request = mock[ExternalContext.Request]

    Mockito when ec.getRequest thenReturn request
    Mockito when ec.getWebAppContext thenReturn webAppContext
    Mockito when webAppContext.attributes thenReturn appAttributes
    Mockito when request.getHeaderValuesMap thenReturn requestAttributes.asJava

    assert(! authorizedWithToken(ec))

    appAttributes += OrbeonTokenLower -> "1234567890"
    assert(! authorizedWithToken(ec))

    requestAttributes += OrbeonTokenLower -> Array("<KEY>")
    assert(! authorizedWithToken(ec))

    requestAttributes += OrbeonTokenLower -> Array("1234567890")
    assert(authorizedWithToken(ec))
  }
}<commit_msg>Refactor: move to ScalaTest<commit_after>/**
 *  Copyright (C) 2012 Orbeon, Inc.
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the
 *  GNU Lesser General Public License as published by the Free Software Foundation; either version
 *  2.1 of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Lesser General Public License for more details.
 *
 *  The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 */
package org.orbeon.oxf.controller

import org.mockito.Mockito
import org.orbeon.oxf.externalcontext.{ExternalContext, WebAppContext}
import org.orbeon.oxf.http.Headers._
import org.scalatest.funspec.AnyFunSpecLike
import org.scalatestplus.mockito.MockitoSugar

import scala.collection.JavaConverters._
import scala.collection.mutable

class AuthorizerTest extends AnyFunSpecLike with MockitoSugar {

  import Authorizer._

  describe("Authorization with token") {

    def buildExternalContextWith(appAttributes: Map[String, AnyRef], reqAttributes: Map[String, Array[String]]): ExternalContext = {

      val ec = mock[ExternalContext]
      val webAppContext = mock[WebAppContext]
      val request = mock[ExternalContext.Request]

      Mockito when ec.getRequest thenReturn request
      Mockito when ec.getWebAppContext thenReturn webAppContext
      Mockito when webAppContext.attributes thenReturn (mutable.HashMap() ++ appAttributes)
      Mockito when request.getHeaderValuesMap thenReturn reqAttributes.asJava

      ec
    }

    it("must not be authorized when no token is set in the app") {
      assert(
        ! authorizedWithToken(
          buildExternalContextWith(Map.empty, Map.empty)
        )
      )
    }

    it("must not be authorized when no token is passed") {
      assert(
        ! authorizedWithToken(
          buildExternalContextWith(Map(OrbeonTokenLower -> "<PASSWORD>0"), Map.empty)
        )
      )
    }

    it("must not be authorized when an incorrect token is passed") {
      assert(
        ! authorizedWithToken(buildExternalContextWith(
          Map(OrbeonTokenLower -> "1234567890"),
          Map(OrbeonTokenLower -> Array("<KEY>")))
        )
      )
    }

    it("must be authorized when the tokens match") {
      assert(
        authorizedWithToken(
          buildExternalContextWith(
            Map(OrbeonTokenLower -> "1234567890"),
            Map(OrbeonTokenLower -> Array("1234567890"))
          )
        )
      )
    }
  }
}
{'fraction_non_alphanumeric': 0.07906482465462274, 'fraction_numerical': 0.013390010626992561, 'mean_word_length': 3.618253189401374, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4938685', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1275, 'n_words': 406}