Skip to content

Commit

Permalink
Merge branch 'main' into 187434370-seed-database
Browse files Browse the repository at this point in the history
  • Loading branch information
ekandell authored Apr 24, 2024
2 parents 2e37ffa + 5f0f654 commit e8ab64a
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 28 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,16 @@ jobs:
rails db:create
rails db:migrate
bundle exec rspec
- name: Download and install Code Climate test reporter
run: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
chmod +x ./cc-test-reporter
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
- name: Run before-build command
run: ./cc-test-reporter before-build
- name: Run after-build command
run: |
./cc-test-reporter after-build --exit-code $?
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ end
group :test do
gem 'rspec-rails'
gem 'guard-rspec'
gem 'simplecov', :require => false
gem 'simplecov', '~> 0.17.1' , :require => false
gem 'codeclimate-test-reporter'
gem 'cucumber-rails', :require => false
gem 'cucumber-rails-training-wheels'
gem 'database_cleaner'
Expand Down
14 changes: 8 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
codeclimate-test-reporter (1.0.7)
simplecov
coderay (1.1.3)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
Expand Down Expand Up @@ -339,12 +341,11 @@ GEM
sprockets-rails
tilt
shellany (0.0.1)
simplecov (0.22.0)
simplecov (0.17.1)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
Expand Down Expand Up @@ -396,6 +397,7 @@ DEPENDENCIES
bootsnap
bootstrap (~> 5.3.2)
byebug
codeclimate-test-reporter
cucumber-rails
cucumber-rails-training-wheels
database_cleaner
Expand All @@ -414,7 +416,7 @@ DEPENDENCIES
rails (~> 7.1.3)
rspec-rails
sassc-rails (~> 2.1)
simplecov
simplecov (~> 0.17.1)
sprockets-rails
stimulus-rails
timecop
Expand Down
18 changes: 17 additions & 1 deletion app/controllers/api/v1/courses_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
module Api
module V1
class CoursesController < BaseController
include ActionController::Flash

def create
render :json => 'The create method of CoursesController is not yet implemented'.to_json, status: 501
course_name = params[:course_name]
existing_course = Course.find_by(course_name: course_name)
if existing_course
render json: { message: 'A course with the same course name already exists.'}, status: :unprocessable_entity
return
end

new_course = Course.create(course_name: course_name)
if new_course.save
flash[:success] = "Course created successfully"
render json: new_course, status: :created
else
flash[:error] = "Failed to save the new course to the database"
render json: new_course.errors, status: :unprocessable_entity
end
end

def index
Expand Down
19 changes: 18 additions & 1 deletion app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
module Api
module V1
class UsersController < BaseController
include ActionController::Flash

def create
render :json => 'the create method of UsersController is not yet implemented'.to_json, status: 501
email = params[:email]

# Check if the user already exists by email
existing_user = User.find_by(email: email)
if existing_user
render json: { message: 'A user with this email already exists.' }, status: :conflict
return
end

# Build a new user object with the given email
new_user = User.new(email: email)

if new_user.save
render json: { message: 'User created successfully', user: new_user }, status: :created
else
render json: { message: 'Failed to create user', errors: new_user.errors.full_messages }, status: :unprocessable_entity
end
end

def index
Expand Down
14 changes: 8 additions & 6 deletions app/models/course.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class Course < ApplicationRecord

#Relationship with User (and UserToCourse)
has_many :user_to_courses
has_many :users, :through => :user_to_courses
# Associations
has_many :course_to_lmss
has_many :lmss, through: :course_to_lmss
has_many :user_to_courses
has_many :users, through: :user_to_courses

# Validations
validates :course_name, presence: true

#Relationship with Lms (and CourseToLms)
has_many :course_to_lmss
has_many :lmss, :through => :course_to_lmss
end
11 changes: 9 additions & 2 deletions app/models/course_to_lms.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
class CourseToLms < ApplicationRecord
belongs_to :lms
belongs_to :course

# Associations
belongs_to :course
belongs_to :lms

# Validations
validates :course_id, presence: true
validates :lms_id, presence: true

end
5 changes: 3 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# app/models/user.rb
class User < ApplicationRecord
validates :email, presence: true, uniqueness: true
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP, message: 'must be a valid email address' }

# Relationship with LmsCredential
# Associations
has_many :lms_credentials, dependent: :destroy

# Relationship with Extension
Expand All @@ -11,4 +12,4 @@ class User < ApplicationRecord
#Relationship with Course (and UserToCourse)
has_many :user_to_courses
has_many :courses, :through => :user_to_courses
end
end
29 changes: 24 additions & 5 deletions spec/controllers/api/v1/courses_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,30 @@
module Api
module V1
describe CoursesController do
describe 'create' do
it 'throws a 501 error' do
post :create
expect(response.status).to eq(501)
describe 'POST #create' do
context "when the new course is successfully created" do
let(:course_name) { "New Course" }

it "creates and saves a new course" do
post :create, params: { course_name: course_name }

expect(response).to have_http_status(:created)
expect(Course.find_by(course_name: course_name)).to be_present
expect(flash[:success]).to eq("Course created successfully")
expect(JSON.parse(response.body)['course_name']).to eq('New Course')
end
end

context "when a course with the same name already exists" do
let!(:existing_course) { Course.create(course_name: "Existing Course") }

it "does not create a new course with the same name and returns an error" do
post :create, params: { course_name: existing_course.course_name }

expect(Course.find_by(course_name: existing_course.course_name)).to be_present
expect(response).to have_http_status(:unprocessable_entity)
expect(JSON.parse(response.body)).to eq({ "message" => "A course with the same course name already exists." })
end
end
end

Expand All @@ -30,6 +50,5 @@ module V1
end
end
end

end
end
43 changes: 39 additions & 4 deletions spec/controllers/api/v1/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,45 @@
module Api
module V1
describe UsersController do
describe 'create' do
it 'throws a 501 error' do
post :create
expect(response.status).to eq(501)
describe 'POST #create' do
context 'when creating a new user' do
it 'creates the user successfully' do
post :create, params: { email: '[email protected]' }

expect(response).to have_http_status(:created)
expect(JSON.parse(response.body)['message']).to eq('User created successfully')
expect(User.exists?(email: '[email protected]')).to be_truthy
end
end

context 'when user with the same email already exists' do
before do
User.create(email: '[email protected]')
end

it 'returns an error message' do
post :create, params: { email: '[email protected]' }

expect(response).to have_http_status(:conflict)
expect(JSON.parse(response.body)['message']).to eq('A user with this email already exists.')
end
end

context 'when email is missing or invalid' do
it 'returns an error when email is missing' do
post :create, params: { email: '' }

expect(response).to have_http_status(:unprocessable_entity)
expect(JSON.parse(response.body)['message']).to eq('Failed to create user')
end

it 'returns an error when email is invalid' do
# Assuming you add email format validation
post :create, params: { email: 'invalid-email' }

expect(response).to have_http_status(:unprocessable_entity)
expect(JSON.parse(response.body)['message']).to eq('Failed to create user')
end
end
end

Expand Down
10 changes: 10 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
# it.
#
# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration

require 'codeclimate-test-reporter'
require 'simplecov'

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
SimpleCov::Formatter::HTMLFormatter,
CodeClimate::TestReporter::Formatter
]
SimpleCov.start 'rails'

RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
Expand Down

0 comments on commit e8ab64a

Please sign in to comment.