From cc74d5ede5628142adf583be204929ba762eb131 Mon Sep 17 00:00:00 2001 From: Dean Lofts Date: Sat, 24 Aug 2024 18:08:35 +1000 Subject: [PATCH] fix tests --- spec/controllers/analytics_controller_spec.rb | 26 ++++++++++++++---- spec/requests/analytics_spec.rb | 27 ++++++++++++++++--- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/spec/controllers/analytics_controller_spec.rb b/spec/controllers/analytics_controller_spec.rb index cb1418a..9e94515 100644 --- a/spec/controllers/analytics_controller_spec.rb +++ b/spec/controllers/analytics_controller_spec.rb @@ -1,4 +1,3 @@ -# spec/controllers/analytics_controller_spec.rb require 'rails_helper' RSpec.describe AnalyticsController, type: :controller do @@ -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) @@ -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 \ No newline at end of file diff --git a/spec/requests/analytics_spec.rb b/spec/requests/analytics_spec.rb index f68a122..12b09b6 100644 --- a/spec/requests/analytics_spec.rb +++ b/spec/requests/analytics_spec.rb @@ -1,7 +1,7 @@ require 'rails_helper' RSpec.describe 'Analytics', type: :request do - describe 'GET /index' do + describe 'GET /:username/analytics' do let(:user) { create(:user) } before do @@ -9,9 +9,28 @@ 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 \ No newline at end of file