<commit_before>module Feature.AuthSpec where

-- {{{ Imports
import Test.Hspec
import Test.Hspec.Wai
import Test.Hspec.Wai.JSON
import Network.HTTP.Types

import SpecHelper
-- }}}

spec :: Spec
spec = beforeAll
  (clearTable "postgrest.auth") . afterAll_ (clearTable "postgrest.auth")
  $ around withApp
  $ describe "authorization" $ do

  it "hides tables that anonymous does not own" $
    get "/authors_only" `shouldRespondWith` 404

  it "returns jwt functions as jwt tokens" $
    post "/rpc/login" [json| { "id": "jdoe", "pass": "<PASSWORD>" } |]
      `shouldRespondWith` ResponseMatcher {
          matchBody = Just [json| {"token":"<KEY>"} |]
        , matchStatus = 200
        , matchHeaders = ["Content-Type" <:> "application/json"]
        }

  it "allows users with permissions to see their tables" $ do
    let auth = authHeaderJWT "<KEY>"
    request methodGet "/authors_only" [auth] ""
      `shouldRespondWith` 200

  it "hides tables from users with invalid JWT" $ do
    let auth = authHeaderJWT "<KEY>"
    request methodGet "/authors_only" [auth] ""
      `shouldRespondWith` 404

  it "recovers after 400 error with logged in user" $ do
    _ <- post "/authors_only" [json| { "owner": "jdoe", "secret": "test content" } |]
    let auth = authHeaderJWT "<KEY>"<commit_msg>Adds another test case for jwt with empty claims<commit_after>module Feature.AuthSpec where

-- {{{ Imports
import Test.Hspec
import Test.Hspec.Wai
import Test.Hspec.Wai.JSON
import Network.HTTP.Types

import SpecHelper
-- }}}

spec :: Spec
spec = beforeAll
  (clearTable "postgrest.auth") . afterAll_ (clearTable "postgrest.auth")
  $ around withApp
  $ describe "authorization" $ do

  it "hides tables that anonymous does not own" $
    get "/authors_only" `shouldRespondWith` 404

  it "returns jwt functions as jwt tokens" $
    post "/rpc/login" [json| { "id": "jdoe", "pass": "<PASSWORD>" } |]
      `shouldRespondWith` ResponseMatcher {
          matchBody = Just [json| {"token":"<KEY>"} |]
        , matchStatus = 200
        , matchHeaders = ["Content-Type" <:> "application/json"]
        }

  it "allows users with permissions to see their tables" $ do
    let auth = authHeaderJWT "<KEY>"
    request methodGet "/authors_only" [auth] ""
      `shouldRespondWith` 200

  it "hides tables from users with invalid JWT" $ do
    let auth = authHeaderJWT "<KEY>"
    request methodGet "/authors_only" [auth] ""
      `shouldRespondWith` 404

  it "hides tables from users with JWT that contain no claims about role" $ do
    let auth = authHeaderJWT "<KEY>"
    request methodGet "/authors_only" [auth] ""
      `shouldRespondWith` 404

  it "recovers after 400 error with logged in user" $ do
    _ <- post "/authors_only" [json| { "owner": "jdoe", "secret": "test content" } |]
    let auth = authHeaderJWT "<KEY>AMSir6Es6WtWx0"
{'fraction_non_alphanumeric': 0.1273817925194072, 'fraction_numerical': 0.012702893436838392, 'mean_word_length': 3.402173913043478, 'pattern_counts': {'":': 10, '<': 18, '<?xml version=': 0, '>': 16, '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': '1041232', 'n_tokens_mistral': 951, 'n_tokens_neox': 884, 'n_words': 283}