<commit_before>require 'spec_helper'

BIG_WARNING = <<~HEREDOC
  MAYBE A WARNING:
  YOU HAVE CHOSEN TO SKIP TESTS FOR USER VERIFICATION.
  If you are a self hosted user that does not want email functionality,
  you can ignore this message.
  If you *do* want to use email in your farmbot setup, consider this a
  failure.
HEREDOC

describe Api::UsersController do
  let(:user) { FactoryBot.create(:user) }
  include Devise::Test::ControllerHelpers
    it 'shows a user record' do
      sign_in user
      get :show, format: :json
      expect(response.status).to eq(200)
      expect(json).to be_kind_of(Array)
      time_stamps = [:created_at, :updated_at]
      expect(json.first.except(*time_stamps))
        .to eq(UserSerializer.new(user).as_json.except(*time_stamps))
      expect(subject.default_serializer_options[:root]).to be false
      expect(subject.default_serializer_options[:user]).to eq(user)
    end

    it 'errors if you try to delete with the wrong password' do
      sign_in user
      delete :destroy, params: { password: "<PASSWORD>!" }, format: :json
      expect(response.status).to eq(422)
      expect(json[:password]).to eq(Users::Destroy::BAD_PASSWORD)
    end

    it 'deletes a user account' do
      sign_in user
      delete :destroy, params: { password: <PASSWORD> }, format: :json
      expect(response.status).to eq(200)
      expect(User.where(id: user.id).count).to eq(0)
    end

    it 'updates user info' do
      sign_in user
      old_pass = user.password
      input = {
        email: "<EMAIL>",
        name: "<NAME>",
        format: :json
      }
      patch :update, params: input
      expect(response.status).to eq(200)
      expect(json[:name]).to eq("<NAME>")
      # Updates to user email require confirmation.
      expect(json[:email]).not_to eq(input[:email])
      expect(json[:email]).to eq(user.email)
    end

    it 'updates password' do
      sign_in user
      old_pass = <PASSWORD>
      input = {
        password: <PASSWORD>,
        new_password: "<PASSWORD>",
        new_password_confirmation: "<PASSWORD>",
        format: :json
      }
      patch :update, params: input
      expect(response.status).to eq(200)
      user.reload<commit_msg>Tests for token revocation part II<commit_after>require 'spec_helper'

BIG_WARNING = <<~HEREDOC
  MAYBE A WARNING:
  YOU HAVE CHOSEN TO SKIP TESTS FOR USER VERIFICATION.
  If you are a self hosted user that does not want email functionality,
  you can ignore this message.
  If you *do* want to use email in your farmbot setup, consider this a
  failure.
HEREDOC

describe Api::UsersController do
  let(:user) { FactoryBot.create(:user) }
  include Devise::Test::ControllerHelpers
    it 'shows a user record' do
      sign_in user
      get :show, format: :json
      expect(response.status).to eq(200)
      expect(json).to be_kind_of(Array)
      time_stamps = [:created_at, :updated_at]
      expect(json.first.except(*time_stamps))
        .to eq(UserSerializer.new(user).as_json.except(*time_stamps))
      expect(subject.default_serializer_options[:root]).to be false
      expect(subject.default_serializer_options[:user]).to eq(user)
    end

    it 'errors if you try to delete with the wrong password' do
      sign_in user
      delete :destroy, params: { password: "<PASSWORD>!" }, format: :json
      expect(response.status).to eq(422)
      expect(json[:password]).to eq(Users::Destroy::BAD_PASSWORD)
    end

    it 'deletes a user account' do
      sign_in user
      delete :destroy, params: { password: <PASSWORD> }, format: :json
      expect(response.status).to eq(200)
      expect(User.where(id: user.id).count).to eq(0)
    end

    it 'updates user info' do
      sign_in user
      old_pass = <PASSWORD>
      input = {
        email: "<EMAIL>",
        name: "<NAME>",
        format: :json
      }
      patch :update, params: input
      expect(response.status).to eq(200)
      expect(json[:name]).to eq("<NAME>")
      # Updates to user email require confirmation.
      expect(json[:email]).not_to eq(input[:email])
      expect(json[:email]).to eq(user.email)
    end

    it 'updates password' do
      TokenIssuance.create(device: user.device,
                           exp:    (Time.now+1.day).to_i,
                           jti:    SecureRandom.uuid)
      sign_in user
      old_pass = <PASSWORD>
      input = {
        password: <PASSWORD>,
        new_password: "<PASSWORD>",
        new_password_confirmation: "<PASSWORD>",
        format: :json
      }
      expect(TokenIssuance.where(device: user.device).count).to be >= 1
      patch :update, params: input
      expect(response.status).to eq(200)
      expect(TokenIssuance.where(device: user.device).count).to be <= 1
      user.reload
{'fraction_non_alphanumeric': 0.11473684210526315, 'fraction_numerical': 0.007368421052631579, 'mean_word_length': 2.8283642224012895, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 23, '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': '4818161', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1456, 'n_words': 400}