Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
loftwah committed Aug 24, 2024
1 parent 235fe95 commit cc74d5e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
26 changes: 21 additions & 5 deletions spec/controllers/analytics_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# spec/controllers/analytics_controller_spec.rb
require 'rails_helper'

RSpec.describe AnalyticsController, type: :controller do
Expand All @@ -11,12 +10,14 @@

describe "GET #index" do
it "returns a success response" do
get :index
get :index, params: { username: user.username }
expect(response).to be_successful
end

it "assigns the correct instance variables" do
get :index
it "sets the correct instance variables" do
get :index, params: { username: user.username }

expect(controller.instance_variable_get(:@user)).to eq(user)
expect(controller.instance_variable_get(:@total_page_views)).to be_a(Integer)
expect(controller.instance_variable_get(:@total_link_clicks)).to be_a(Integer)
expect(controller.instance_variable_get(:@total_achievement_views)).to be_a(Integer)
Expand All @@ -27,5 +28,20 @@
expect(controller.instance_variable_get(:@daily_views)).to be_a(Hash)
expect(controller.instance_variable_get(:@browser_data)).to be_a(Hash)
end

context "when viewing another user's analytics" do
let(:other_user) { create(:user, public_analytics: true) }

it "allows viewing public analytics" do
get :index, params: { username: other_user.username }
expect(response).to be_successful
end

it "redirects when trying to view private analytics" do
other_user.update(public_analytics: false)
get :index, params: { username: other_user.username }
expect(response).to redirect_to(root_path)
end
end
end
end
end
27 changes: 23 additions & 4 deletions spec/requests/analytics_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
require 'rails_helper'

RSpec.describe 'Analytics', type: :request do
describe 'GET /index' do
describe 'GET /:username/analytics' do
let(:user) { create(:user) }

before do
sign_in user
end

it 'returns http success' do
get analytics_path
puts response.body # Add this line to print the response body in case of an error
get user_analytics_path(username: user.username)
expect(response).to have_http_status(:success)
end

it 'renders the correct content' do
get user_analytics_path(username: user.username)
expect(response.body).to include('Analytics') # Adjust this to match your actual content
end

context "when viewing another user's analytics" do
let(:other_user) { create(:user, public_analytics: true) }

it "allows viewing public analytics" do
get user_analytics_path(username: other_user.username)
expect(response).to have_http_status(:success)
end

it "redirects when trying to view private analytics" do
other_user.update(public_analytics: false)
get user_analytics_path(username: other_user.username)
expect(response).to redirect_to(root_path)
end
end
end
end
end

0 comments on commit cc74d5e

Please sign in to comment.