<commit_before>package com.github.jancajthaml.jwt

import com.github.jancajthaml.json.{jsondumps, jsonloads}

//import collection.mutable.Stack
import org.scalatest._

import java.util.Base64
import scala.util.{Try,Success,Failure}

class JSONSpecs extends FlatSpec with Matchers {

  val secret: String = "secret"

  def decodeB64Native(x: String): String = new String(java.util.Base64.getDecoder().decode(x))

  "encode" should "have valid header" in {
    encode(Map(), "HS256", secret) match {
      case Success(x) => {
        val token = x
        val parts = x.split("\\.")

        parts should have size (3)

        val header = jsonloads(decodeB64Native(parts(0)))
        header.keys should have size (2)
        header.getOrElse("alg", None) should !== (None)
        header.getOrElse("typ", None) should === ("JWT")

      }
      case Failure(f) => {
        false should === (true)
        //@todo reject test now
      }
    }

  }

  it should "have valid payload" in {
    val payload = Map(
      "x" -> "y"
    )

    encode(payload, "HS256", secret) match {
      case Success(x) => {
        val token = x
        val parts = x.split("\\.")

        parts should have size (3)

        val payloadDecoded = jsonloads(decodeB64Native(parts(1)))

        payloadDecoded.keys should have size (payload.keys.size + 1)
        payloadDecoded.getOrElse("x", None) should === ("y")
      }
      case Failure(f) => {
        false should === (true)
        //@todo reject test now
      }
    }
  }

  "signature" should "support HS256" in {
    val algorithm: String = "HS256"

    encode(Map(), algorithm, secret) match {
      case Success(x) => {
        val token = x
        val parts = x.split("\\.")

        parts should have size (3)

        val header = jsonloads(decodeB64Native(parts(0)))

        header.getOrElse("alg", None) should === (algorithm)
      }
      case Failure(f) => {
        false should === (true)
        //@todo reject test now
      }
    }
  }

  it should "support HS384" in {
    val algorithm: String = "HS384"

    encode(Map(), algorithm, secret) match {
      case Success(x) => {
        val token = x
        val parts = x.split("\\.")

        parts should have size (3)

        val header = jsonloads(decodeB64Native(parts(0)))

        header.getOrElse("alg", None) should === (algorithm)
      }
      case Failure(f) => {
        false should === (true)
        //@todo reject test now
      }
    }    
  }

  it should "support HS512" in {
    val algorithm: String = "HS512"

    encode(Map(), algorithm, secret) match {
      case Success(x) => {
        val token = x
        val parts = x.split("\\.")

        parts should have size (3)

        val header = jsonloads(decodeB64Native(parts(0)))

        header.getOrElse("alg", None) should === (algorithm)
      }
      case Failure(f) => {
        false should === (true)
        //@todo reject test now
      }
    }
  }

  //RS256
  //RS384
  //RS512

  //ES256
  //ES384
  //ES512

  "decode" should "validate expiration" in {
    val token = "<KEY>"

    decode(token, secret) match {
      case Success(x) => {
        false should === (true)
      }
      case Failure(f) => {
        true should === (true)
      }
    }
  }

  it should "validate issued at" in {
    val token = "<KEY>"

    decode(token, secret) match {
      case Success(x) => {
        false should === (true)
      }
      case Failure(f) => {
        true should === (true)
      }
    }
  }

  it should "validate use not before" in {
    val token = "<KEY>"
    
    decode(token, secret) match {
      case Success(x) => {
        false should === (true)
      }
      case Failure(f) => {
        true should === (true)
      }
    }
  }
  

}<commit_msg>updated specification tests<commit_after>package com.github.jancajthaml.jwt

import com.github.jancajthaml.json.{jsondumps, jsonloads}
import org.scalatest.{FlatSpec, Matchers}
import java.util.Base64
import scala.util.{Try,Success,Failure}

class JWTSpecs extends FlatSpec with Matchers {

  val secret: String = "secret"

  def decodeB64Native(x: String): String = new String(java.util.Base64.getDecoder().decode(x))

  "encode" should "have valid header" in {
    encode(Map(), "HS256", secret) match {
      case Success(x) => {
        val token = x
        val parts = x.split("\\.")

        parts should have size (3)

        val header = jsonloads(decodeB64Native(parts(0)))
        header.keys should have size (2)
        header.getOrElse("alg", None) should !== (None)
        header.getOrElse("typ", None) should === ("JWT")

      }
      case Failure(f) => {
        false should === (true)
        //@todo reject test now
      }
    }

  }

  it should "have valid payload" in {
    val payload = Map(
      "x" -> "y"
    )

    encode(payload, "HS256", secret) match {
      case Success(x) => {
        val token = x
        val parts = x.split("\\.")

        parts should have size (3)

        val payloadDecoded = jsonloads(decodeB64Native(parts(1)))

        payloadDecoded.keys should have size (payload.keys.size + 1)
        payloadDecoded.getOrElse("x", None) should === ("y")
      }
      case Failure(f) => {
        false should === (true)
        //@todo reject test now
      }
    }
  }

  "signature" should "support HS256" in {
    val algorithm: String = "HS256"

    encode(Map(), algorithm, secret) match {
      case Success(x) => {
        val token = x
        val parts = x.split("\\.")

        parts should have size (3)

        val header = jsonloads(decodeB64Native(parts(0)))

        header.getOrElse("alg", None) should === (algorithm)
      }
      case Failure(f) => {
        false should === (true)
        //@todo reject test now
      }
    }
  }

  it should "support HS384" in {
    val algorithm: String = "HS384"

    encode(Map(), algorithm, secret) match {
      case Success(x) => {
        val token = x
        val parts = x.split("\\.")

        parts should have size (3)

        val header = jsonloads(decodeB64Native(parts(0)))

        header.getOrElse("alg", None) should === (algorithm)
      }
      case Failure(f) => {
        false should === (true)
        //@todo reject test now
      }
    }    
  }

  it should "support HS512" in {
    val algorithm: String = "HS512"

    encode(Map(), algorithm, secret) match {
      case Success(x) => {
        val token = x
        val parts = x.split("\\.")

        parts should have size (3)

        val header = jsonloads(decodeB64Native(parts(0)))

        header.getOrElse("alg", None) should === (algorithm)
      }
      case Failure(f) => {
        false should === (true)
        //@todo reject test now
      }
    }
  }

  //RS256
  //RS384
  //RS512

  //ES256
  //ES384
  //ES512

}

class ClaimsSpecs extends FlatSpec with Matchers {

  val secret: String = "secret"

  "decode" should "validate expiration" in {
    val token = "<KEY>"

    decode(token, secret) match {
      case Success(x) => {
        false should === (true)
      }
      case Failure(f) => {
        true should === (true)
      }
    }
  }

  it should "validate issued at" in {
    val token = "<KEY>"

    decode(token, secret) match {
      case Success(x) => {
        false should === (true)
      }
      case Failure(f) => {
        true should === (true)
      }
    }
  }

  it should "validate use not before" in {
    val token = "<KEY>"
    
    decode(token, secret) match {
      case Success(x) => {
        false should === (true)
      }
      case Failure(f) => {
        true should === (true)
      }
    }
  }

}

{'fraction_non_alphanumeric': 0.1400262467191601, 'fraction_numerical': 0.01837270341207349, 'mean_word_length': 1.988627450980392, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '931681', 'n_tokens_mistral': 2542, 'n_tokens_neox': 2369, 'n_words': 661}