Skip to content

Commit

Permalink
updated test cases in csv_controller_spec.rb and ensured passed, incl…
Browse files Browse the repository at this point in the history
…uded test.csv for testing purposes
  • Loading branch information
BenFan1002 committed Oct 5, 2024
1 parent 3fd9d50 commit 1896e6a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
6 changes: 2 additions & 4 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
describe '#current_user' do
context 'when session contains user_id' do
before do
@user = User.create!(uid: '12345', provider: 'google_oauth2', email: '[email protected]', first_name: 'John',
last_name: 'Doe')
@user = User.create!(uid: '12345', provider: 'google_oauth2', email: '[email protected]', first_name: 'John', last_name: 'Doe')
session[:user_id] = @user.id
puts "session[:user_id] = #{session[:user_id]}"
end
Expand All @@ -32,8 +31,7 @@
describe '#logged_in?' do
context 'when user is logged in' do
before do
@user = User.create!(uid: '12345', provider: 'google_oauth2', email: '[email protected]', first_name: 'John',
last_name: 'Doe')
@user = User.create!(uid: '12345', provider: 'google_oauth2', email: '[email protected]', first_name: 'John', last_name: 'Doe')
session[:user_id] = @user.id
end

Expand Down
34 changes: 34 additions & 0 deletions spec/controllers/csv_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# spec/controllers/csv_controller_spec.rb
require 'rails_helper'

RSpec.describe CsvController, type: :controller do
before do
# Create a user and set session user_id as done in your application_controller_spec.rb
@user = User.create!(uid: '12345', provider: 'google_oauth2', email: '[email protected]', first_name: 'John', last_name: 'Doe')
session[:user_id] = @user.id
end

let(:file) { fixture_file_upload(Rails.root.join('spec', 'fixtures', 'files', 'test.csv'), 'text/csv') }

describe "POST #upload" do
context "with a valid CSV file" do
it "processes the CSV file, sets a success flash, and redirects to the user's page" do
post :upload, params: { csv_file: file }

expect(response).to have_http_status(:redirect)
expect(response).to redirect_to(user_path(@user)) # Redirect to the current user's page
expect(flash[:notice]).to eq("CSV file uploaded successfully.")
end
end

context "when no CSV file is selected" do
it "sets an error flash and redirects to the user's page" do
post :upload, params: { csv_file: nil }

expect(response).to have_http_status(:redirect)
expect(response).to redirect_to(user_path(@user)) # Redirect to the current user's page
expect(flash[:error]).to eq("Please upload a CSV file.")
end
end
end
end
5 changes: 5 additions & 0 deletions spec/fixtures/files/test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Name,Age,Occupation
Alice,25,Engineer
Bob,30,Doctor
Charlie,35,Artist
Diana,40,Scientist

0 comments on commit 1896e6a

Please sign in to comment.