Skip to content

Commit

Permalink
Merge pull request #35 from loftwah/dl/baseline-tests
Browse files Browse the repository at this point in the history
Dl/baseline tests
  • Loading branch information
loftwah authored Aug 23, 2024
2 parents bbfe412 + c375a66 commit e8a6880
Show file tree
Hide file tree
Showing 20 changed files with 483 additions and 16 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ group :development, :test do
gem 'debug', platforms: %i[mri windows]
gem 'factory_bot_rails'
gem 'rspec-rails', '~> 6.0.0'
gem 'shoulda-matchers'
end

group :development do
Expand All @@ -80,4 +81,5 @@ group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
gem 'capybara'
gem 'selenium-webdriver'
gem 'rails-controller-testing'
end
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ GEM
activesupport (= 7.1.3.4)
bundler (>= 1.15.0)
railties (= 7.1.3.4)
rails-controller-testing (1.0.5)
actionpack (>= 5.0.1.rc1)
actionview (>= 5.0.1.rc1)
activesupport (>= 5.0.1.rc1)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
Expand Down Expand Up @@ -295,6 +299,8 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
shoulda-matchers (6.4.0)
activesupport (>= 5.2.0)
sidekiq (7.3.1)
concurrent-ruby (< 2)
connection_pool (>= 2.3.0)
Expand Down Expand Up @@ -390,9 +396,11 @@ DEPENDENCIES
mini_magick
puma (>= 5.0)
rails (~> 7.1.3, >= 7.1.3.4)
rails-controller-testing
redis (>= 4.0.1)
rspec-rails (~> 6.0.0)
selenium-webdriver
shoulda-matchers
sidekiq
sidekiq-scheduler
sprockets-rails
Expand Down
11 changes: 7 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# app/models/user.rb
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
Expand All @@ -10,10 +11,10 @@ class User < ApplicationRecord
has_many :link_clicks
has_many :achievement_views

validates :username, presence: true, uniqueness: true
validates :username, uniqueness: true, allow_blank: true
validates :full_name, presence: true
validate :ensure_username_presence

before_validation :set_default_username, on: :create
after_save :generate_open_graph_image, unless: -> { Rails.env.test? }
after_save :download_and_store_avatar

Expand Down Expand Up @@ -62,7 +63,9 @@ def download_and_store_avatar

private

def set_default_username
self.username ||= email.split('@').first
def ensure_username_presence
if username.blank?
self.username = email.present? ? email.split('@').first : "user#{SecureRandom.hex(4)}"
end
end
end
80 changes: 80 additions & 0 deletions spec/controllers/achievements_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# spec/controllers/achievements_controller_spec.rb
require 'rails_helper'

RSpec.describe AchievementsController, type: :controller do
let(:user) { create(:user) }
let(:achievement) { create(:achievement, user: user) }

describe "GET #index" do
it "returns a success response" do
get :index
expect(response).to be_successful
end
end

describe "GET #show" do
context "when achievement doesn't have a URL" do
it "returns a success response" do
get :show, params: { id: achievement.to_param }
expect(response).to be_successful
end
end

context "when achievement has a URL" do
let(:achievement_with_url) { create(:achievement, user: user, url: 'http://example.com') }

it "redirects to the achievement URL" do
get :show, params: { id: achievement_with_url.to_param }
expect(response).to redirect_to(achievement_with_url.url)
end
end

it "creates an AchievementView" do
expect {
get :show, params: { id: achievement.to_param }
}.to change(AchievementView, :count).by(1)
end
end

describe "GET #new" do
it "returns a success response" do
sign_in user
get :new
expect(response).to be_successful
end
end

describe "POST #create" do
context "with valid params" do
it "creates a new Achievement" do
sign_in user
expect {
post :create, params: { achievement: attributes_for(:achievement) }
}.to change(Achievement, :count).by(1)
end
end
end

describe "PUT #update" do
context "with valid params" do
let(:new_attributes) { { title: "New Title" } }

it "updates the requested achievement" do
sign_in user
put :update, params: { id: achievement.to_param, achievement: new_attributes }
achievement.reload
expect(achievement.title).to eq("New Title")
end
end
end

describe "DELETE #destroy" do
it "destroys the requested achievement" do
sign_in user
achievement # ensure achievement is created before the expect block
expect {
delete :destroy, params: { id: achievement.to_param }
}.to change(Achievement, :count).by(-1)
end
end
end
31 changes: 31 additions & 0 deletions spec/controllers/analytics_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# spec/controllers/analytics_controller_spec.rb
require 'rails_helper'

RSpec.describe AnalyticsController, type: :controller do
let(:user) { create(:user) }

before do
sign_in user
create(:daily_metric, user: user, date: Date.today)
end

describe "GET #index" do
it "returns a success response" do
get :index
expect(response).to be_successful
end

it "assigns the correct instance variables" do
get :index
expect(assigns(:total_page_views)).to be_a(Integer)
expect(assigns(:total_link_clicks)).to be_a(Integer)
expect(assigns(:total_achievement_views)).to be_a(Integer)
expect(assigns(:unique_visitors)).to be_a(Integer)
expect(assigns(:latest_daily_metric)).to be_a(DailyMetric)
expect(assigns(:link_analytics)).to be_an(Array)
expect(assigns(:achievement_analytics)).to be_an(Array)
expect(assigns(:daily_views)).to be_a(Hash)
expect(assigns(:browser_data)).to be_a(Hash)
end
end
end
91 changes: 91 additions & 0 deletions spec/controllers/links_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# spec/controllers/links_controller_spec.rb
require 'rails_helper'

RSpec.describe LinksController, type: :controller do
let(:user) { create(:user) }
let(:link) { create(:link, user: user) }

describe "GET #index" do
it "returns a success response" do
get :index
expect(response).to be_successful
end
end

describe "GET #show" do
it "returns a success response" do
get :show, params: { id: link.to_param }
expect(response).to be_successful
end
end

describe "GET #new" do
it "returns a success response" do
sign_in user
get :new
expect(response).to be_successful
end
end

describe "POST #create" do
context "with valid params" do
it "creates a new Link" do
sign_in user
expect {
post :create, params: { link: attributes_for(:link) }
}.to change(Link, :count).by(1)
end
end
end

describe "PUT #update" do
context "with valid params" do
let(:new_attributes) { { title: "New Title" } }

it "updates the requested link" do
sign_in user
put :update, params: { id: link.to_param, link: new_attributes }
link.reload
expect(link.title).to eq("New Title")
end
end
end

describe "DELETE #destroy" do
it "destroys the requested link" do
sign_in user
link # ensure link is created before the expect block
expect {
delete :destroy, params: { id: link.to_param }
}.to change(Link, :count).by(-1)
end
end

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

it "assigns the correct instance variables" do
get :user_links, params: { username: user.username }
expect(assigns(:user)).to eq(user)
expect(assigns(:links)).to be_an(ActiveRecord::Relation)
expect(assigns(:pinned_links)).to be_an(ActiveRecord::Relation)
expect(assigns(:achievements)).to be_an(ActiveRecord::Relation)
end
end

describe "GET #track_click" do
it "creates a LinkClick" do
expect {
get :track_click, params: { id: link.to_param }
}.to change(LinkClick, :count).by(1)
end

it "redirects to the link url" do
get :track_click, params: { id: link.to_param }
expect(response).to redirect_to(link.url)
end
end
end
11 changes: 11 additions & 0 deletions spec/controllers/pages_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# spec/controllers/pages_controller_spec.rb
require 'rails_helper'

RSpec.describe PagesController, type: :controller do
describe "GET #home" do
it "returns a success response" do
get :home
expect(response).to be_successful
end
end
end
50 changes: 50 additions & 0 deletions spec/controllers/users/registrations_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# spec/controllers/users/registrations_controller_spec.rb
require 'rails_helper'

RSpec.describe Users::RegistrationsController, type: :controller do
before do
@request.env["devise.mapping"] = Devise.mappings[:user]
end

describe "POST #create" do
let(:valid_attributes) {
{ email: "[email protected]", password: "password", password_confirmation: "password",
username: "testuser", full_name: "Test User", tags: "tag1,tag2" }
}

it "creates a new User" do
expect {
post :create, params: { user: valid_attributes }
}.to change(User, :count).by(1)
end

it "correctly processes tags" do
post :create, params: { user: valid_attributes }
user = User.last
tags = user.tags.is_a?(String) ? JSON.parse(user.tags) : user.tags
expect(tags).to eq(["tag1", "tag2"])
end
end

describe "PUT #update" do
let(:user) { create(:user, tags: ["old_tag1", "old_tag2"].to_json) }

before do
sign_in user
end

context "with valid params" do
let(:new_attributes) {
{ full_name: "New Name", tags: "new_tag1,new_tag2" }
}

it "updates the requested user" do
put :update, params: { user: new_attributes }
user.reload
expect(user.full_name).to eq("New Name")
tags = user.tags.is_a?(String) ? JSON.parse(user.tags) : user.tags
expect(tags).to eq(["new_tag1", "new_tag2"])
end
end
end
end
11 changes: 11 additions & 0 deletions spec/factories/achievements.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# spec/factories/achievements.rb
FactoryBot.define do
factory :achievement do
title { "My Achievement" }
date { Date.today }
description { "This is a test achievement" }
icon { "fa-trophy" }
url { nil }
association :user
end
end
11 changes: 11 additions & 0 deletions spec/factories/daily_metrics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# spec/factories/daily_metrics.rb
FactoryBot.define do
factory :daily_metric do
user
date { Date.today }
page_views { 10 }
link_clicks { 5 }
achievement_views { 3 }
unique_visitors { 2 }
end
end
10 changes: 10 additions & 0 deletions spec/factories/links.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# spec/factories/links.rb
FactoryBot.define do
factory :link do
title { "MyString" }
url { "http://example.com" }
visible { true }
pinned { false }
association :user
end
end
Loading

0 comments on commit e8a6880

Please sign in to comment.