diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index fae9073..b9601a7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 }} \ No newline at end of file diff --git a/Gemfile b/Gemfile index 63d10af..70df564 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index da59c08..6599590 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -396,6 +397,7 @@ DEPENDENCIES bootsnap bootstrap (~> 5.3.2) byebug + codeclimate-test-reporter cucumber-rails cucumber-rails-training-wheels database_cleaner @@ -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 diff --git a/app/controllers/api/v1/courses_controller.rb b/app/controllers/api/v1/courses_controller.rb index cc44268..e763b79 100644 --- a/app/controllers/api/v1/courses_controller.rb +++ b/app/controllers/api/v1/courses_controller.rb @@ -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 diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index d1be389..2383e24 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -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 diff --git a/app/models/course.rb b/app/models/course.rb index 5752360..41de79c 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -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 \ No newline at end of file diff --git a/app/models/course_to_lms.rb b/app/models/course_to_lms.rb index 6f55d00..8496251 100644 --- a/app/models/course_to_lms.rb +++ b/app/models/course_to_lms.rb @@ -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 \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 9fa39ef..c88fc18 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 @@ -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 \ No newline at end of file diff --git a/spec/controllers/api/v1/courses_controller_spec.rb b/spec/controllers/api/v1/courses_controller_spec.rb index 08452f5..318f5ba 100644 --- a/spec/controllers/api/v1/courses_controller_spec.rb +++ b/spec/controllers/api/v1/courses_controller_spec.rb @@ -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 @@ -30,6 +50,5 @@ module V1 end end end - end end diff --git a/spec/controllers/api/v1/users_controller_spec.rb b/spec/controllers/api/v1/users_controller_spec.rb index 771c424..4f911ca 100644 --- a/spec/controllers/api/v1/users_controller_spec.rb +++ b/spec/controllers/api/v1/users_controller_spec.rb @@ -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: 'test@example.com' } + + expect(response).to have_http_status(:created) + expect(JSON.parse(response.body)['message']).to eq('User created successfully') + expect(User.exists?(email: 'test@example.com')).to be_truthy + end + end + + context 'when user with the same email already exists' do + before do + User.create(email: 'existing@example.com') + end + + it 'returns an error message' do + post :create, params: { email: 'existing@example.com' } + + 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 327b58e..53d8c52 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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