<commit_before># frozen_string_literal: true

require 'rails_helper'

RSpec.describe UserAuth, type: :controller do
  # make fake controller for testing
  controller(V1::ApiController) do
    include UserAuth

    def fake_action
      render_success
    end
  end

  before do
    routes.draw do
      get 'fake_action' => 'v1/api#fake_action'
    end
  end

  let(:user) do
    a = create(:user)
    a.generate_auth_token!
    a.save

    a
  end

  it 'errors when auth header is not present' do
    get :fake_action

    expect(response.status).to eq(401)
    expect(json).to include('error' => 'authorization required')
  end

  it 'errors when auth token is nil' do
    # create applicant with nil auth token to try and trick it
    create(:user, auth_token: nil)

    request.headers['Authorization'] = 'Bearer'

    get :fake_action

    expect(response.status).to eq(401)
    expect(json).to include('error' => 'authorization invalid')
  end

  it 'errors when auth token is incorrect' do
    request.headers['Authorization'] = 'Bearer notarealtoken'

    get :fake_action

    expect(response.status).to eq(401)
    expect(json).to include('error' => 'authorization invalid')
  end

  it 'succeeds with valid auth token' do
    request.headers['Authorization'] = "Bearer #{user.auth_token}"

    get :fake_action

    expect(response.status).to eq(200)
    expect(json).to include('success' => true)
  end

  it 'errors when auth token is older than 3 days' do
    user.auth_token_generation -= 3.day
    user.save

    request.headers['Authorization'] = "Bearer #{user.auth_token}"

    get :fake_action

    expect(response.status).to eq(401)
    expect(json).to include('error' => 'auth token expired')
  end

  context 'when specifying specific methods to authorize' do<commit_msg>3 day -> 3 days<commit_after># frozen_string_literal: true

require 'rails_helper'

RSpec.describe UserAuth, type: :controller do
  # make fake controller for testing
  controller(V1::ApiController) do
    include UserAuth

    def fake_action
      render_success
    end
  end

  before do
    routes.draw do
      get 'fake_action' => 'v1/api#fake_action'
    end
  end

  let(:user) do
    a = create(:user)
    a.generate_auth_token!
    a.save

    a
  end

  it 'errors when auth header is not present' do
    get :fake_action

    expect(response.status).to eq(401)
    expect(json).to include('error' => 'authorization required')
  end

  it 'errors when auth token is nil' do
    # create applicant with nil auth token to try and trick it
    create(:user, auth_token: nil)

    request.headers['Authorization'] = 'Bearer'

    get :fake_action

    expect(response.status).to eq(401)
    expect(json).to include('error' => 'authorization invalid')
  end

  it 'errors when auth token is incorrect' do
    request.headers['Authorization'] = 'Bearer notarealtoken'

    get :fake_action

    expect(response.status).to eq(401)
    expect(json).to include('error' => 'authorization invalid')
  end

  it 'succeeds with valid auth token' do
    request.headers['Authorization'] = "Bearer #{user.auth_token}"

    get :fake_action

    expect(response.status).to eq(200)
    expect(json).to include('success' => true)
  end

  it 'errors when auth token is older than 3 days' do
    user.auth_token_generation -= 3.days
    user.save

    request.headers['Authorization'] = "Bearer #{user.auth_token}"

    get :fake_action

    expect(response.status).to eq(401)
    expect(json).to include('error' => 'auth token expired')
  end

  context 'when specifying specific methods to authorize' do
{'fraction_non_alphanumeric': 0.1040912886167548, 'fraction_numerical': 0.011132758140829391, 'mean_word_length': 3.3829268292682926, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3129841', 'n_tokens_mistral': 1168, 'n_tokens_neox': 1086, 'n_words': 332}