Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Part2 - Test Coverage #46

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ jobs:
steps:
- install-dependencies
- setup-database
- run: bundle exec rails tailwindcss:build
- run:
command: bundle exec rspec --format progress --format RspecJunitFormatter -o ~/rspec/rspec.xml
command: bundle exec rspec --format progress --format RspecJunitFormatter -o ~/test_results/rspec/rspec.xml
when: always
- store_test_results:
path: ~/rspec
path: ~/test_results
4 changes: 0 additions & 4 deletions app/bot_tools/bot_tool.rb

This file was deleted.

1 change: 0 additions & 1 deletion app/controllers/bots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def update
end
end


private

def bot_params
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/chats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ def show
end

def readonly
@chat = Chat.find(params[:id])
if @chat.public_access?
@chat = Chat.find_by(id: params[:id])

if @chat&.public_access?
render :show
else
redirect_to root_path, notice: "Chat not found"
Expand All @@ -69,6 +70,4 @@ def readonly
def chat_params
params.require(:chat).permit(:first_message, :engine, :bot_id)
end


end
1 change: 1 addition & 0 deletions app/models/setting_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def initialize(current_user, form, key, value)
@form = form
@current_user = current_user
@key = key
@value = value
end

def label
Expand Down
31 changes: 31 additions & 0 deletions spec/constraints/admin_constraint_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'rails_helper'

describe AdminConstraint, type: :routing do
subject { described_class.matches?(request) }

let(:request) { double('request') }

before do
allow(request).to receive(:session).and_return({
user_id: user&.id
})
end

context 'when user is not present' do
let(:user) { nil }

it { is_expected.to eq false }
end

context 'when user is not admin' do
let(:user) { create(:user) }

it { is_expected.to eq false }
end

context 'when user is admin' do
let(:user) { create(:admin) }

it { is_expected.to eq true }
end
end
2 changes: 1 addition & 1 deletion spec/factories/bot.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FactoryBot.define do
factory :bot do
goals { {} }
name { 'My Bot' }
name { Faker::Name.name }
settings { {} }
end
end
5 changes: 5 additions & 0 deletions spec/factories/chats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
factory :chat do
engine { 'engine' }
analysis { {} }
public_access { false }

association :bot
association :user
Expand All @@ -38,5 +39,9 @@
after(:create) do |chat, evaluator|
create_list(:message, evaluator.message_count, chat: chat)
end

trait :public do
public_access { true }
end
end
end
1 change: 1 addition & 0 deletions spec/factories/messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
factory :message do
role { 'user' }
content { Faker::Hipster.sentence }
tokens_count { rand(1..100) }

association :chat
end
Expand Down
10 changes: 5 additions & 5 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
email { Faker::Internet.email }
name { Faker::Name.name }

oauth_uid { Faker::Number.number(digits: 21) }
oauth_provider { 'Google' }
oauth_uid { Faker::Number.number(digits: 9).to_s }
oauth_provider { 'google_oauth2' }

image_url { Faker::Internet.url }
end

trait :admin do
admin { true }
end
factory :admin, parent: :user do
admin { true }
end
end
8 changes: 8 additions & 0 deletions spec/models/chat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,12 @@
end
end
end

describe '#total_token_count' do
let(:chat) { build(:chat, message_count: 5) }

it 'sums up messages token_count' do
expect(chat.total_token_count).to eq chat.messages.sum(:tokens_count)
end
end
end
6 changes: 6 additions & 0 deletions spec/models/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@
end
end

describe '#to_partial_path' do
subject(:result) { described_class.new.to_partial_path }

it { is_expected.to eq 'messages/message' }
end

describe '#reanalyze' do
let(:message) { create(:message) }
let(:chat) { message.chat }
Expand Down
8 changes: 8 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
require 'simplecov'

SimpleCov.start 'rails' do
add_group 'Services', 'app/services'
add_group 'Reflexes', 'app/reflexes'
add_group 'Constraints', 'app/constraints'
end

ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
# Prevent database truncation if the environment is production
Expand Down
103 changes: 103 additions & 0 deletions spec/requests/chats_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
require 'rails_helper'

RSpec.describe ChatsController, type: :request do
before { allow(Gpt).to receive(:chat) }

shared_examples_for 'logged out' do
context 'when logged out' do
let(:user) { nil }

it 'redirects to home' do
expect(response).to redirect_to root_url
end
end
end

describe 'GET /index' do
before do
login_user(user)

get chats_url
end

it_behaves_like 'logged out'

context 'when user has chats' do
let(:user) do
user = create(:user)
create(:chat, user: user)

user
end

it 'responds with http 200' do
expect(response).to have_http_status :ok
end
end

context 'when user has no chats' do
let(:user) { create(:user) }

it 'redirects to chats' do
expect(response).to redirect_to new_chat_url
end
end
end

describe 'GET /chats/:id' do
let(:user) { chat.user }
let(:chat) { create(:chat) }

before do
login_user(user)

get chats_url(chat)
end

it_behaves_like 'logged out'

it 'responds with http 200' do
expect(response).to have_http_status :ok
end
end

describe 'read only chat' do
let(:chat) { create(:chat, :public) }

shared_examples_for 'readonly requests' do
it 'responds with http 200' do
expect(response).to have_http_status :ok
end

context 'when chat is not public' do
let(:chat) { create(:chat, public_access: false) }

it 'redirects to /', :aggregate_failures do
expect(response).to redirect_to root_path
expect(flash[:notice]).to eq 'Chat not found'
end
end

context 'when chat is not found' do
let(:chat) { instance_double(Chat, id: '1234') }

it 'redirects to /', :aggregate_failures do
expect(response).to redirect_to root_path
expect(flash[:notice]).to eq 'Chat not found'
end
end
end

describe 'GET /chats/:id/readonly' do
before { get readonly_chat_url(id: chat.id) }

it_behaves_like 'readonly requests'
end

describe 'GET /c/:id' do
before { get readonly_url(id: chat.id) }

it_behaves_like 'readonly requests'
end
end
end
27 changes: 27 additions & 0 deletions spec/requests/home_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'rails_helper'

RSpec.describe HomeController, type: :request do
before { allow(Gpt).to receive(:chat) }

describe 'GET /index' do
let(:user) { nil }

before do
login_user(user)

get home_index_url
end

it 'responds with http 200' do
expect(response).to have_http_status :ok
end

context 'when logged in' do
let(:user) { create(:user) }

it 'redirects to chats' do
expect(response).to redirect_to chats_url
end
end
end
end
30 changes: 30 additions & 0 deletions spec/requests/sessions_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'rails_helper'

RSpec.describe SessionsController, type: :request do
describe 'GET /auth/:provider/callback' do
let(:params) { { provider: :google } }
let(:user) { create(:user) }

before do
mock_omniauth(user)

get '/auth/:provider/callback', params: params
end

it 'signs the user in', :aggregate_failures do
expect(response).to redirect_to '/chats'
expect(flash[:notice]).to eq 'Signed in!'
expect(session[:user_id]).to eq user.id
end
end

describe 'GET /logout' do
it 'signs the user out' do
get logout_url

expect(response).to redirect_to root_url
expect(flash[:notice]).to eq 'Signed out!'
expect(session[:user_id]).to eq nil
end
end
end
32 changes: 32 additions & 0 deletions spec/requests/settings_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'rails_helper'

RSpec.describe "Settings", type: :request do
let(:user) { create(:user) }

before { login_user(user) }

describe "GET /show" do
xit "returns http success" do
get "/settings/show"
expect(response).to have_http_status(:success)
end
end

describe 'PATCH /settings' do
let(:params) do
{
user: {
preferred_language: 'Baby Talk'
}
}
end

it 'updates user settings', :aggregate_failures do
patch settings_url, params: params

expect(response).to redirect_to settings_path
expect(flash[:notice]).to eq('Settings updated')
expect(user.reload.settings.new_setting).to eq params.dig(:user, :new_setting)
end
end
end
Loading