forked from saasbook/flextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented add_user in courses controller
- Loading branch information
1 parent
5f0f654
commit 004fdb1
Showing
3 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class UserToCourse < ApplicationRecord | ||
# Associations | ||
belongs_to :user | ||
belongs_to :course | ||
|
||
# Validations | ||
validates :user_id, presence: true | ||
validates :course_id, presence: true | ||
validates :role, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,9 +44,36 @@ module V1 | |
end | ||
|
||
describe 'add_user' do | ||
it 'throws a 501 error' do | ||
put :add_user, params: { course_id: 16, user_id: 16 } | ||
expect(response.status).to eq(501) | ||
let(:test_course) { Course.create(course_name: "Test Course") } | ||
let(:test_user) { User.create(email: "[email protected]") } | ||
|
||
context "Provided parameters are valid" do | ||
it "adds an existing user to an existing course" do | ||
post :add_user, params: { course_id: test_course.id, user_id: test_user.id, role: "ta" } | ||
expect(response).to have_http_status(:created) | ||
expect(flash["success"]).to eq("User added to the course successfully.") | ||
end | ||
end | ||
|
||
context "Provided parameter are invalid" do | ||
it "returns an error if course is not existed in the courses table" do | ||
post :add_user, params: { course_id: 123456, user_id: test_user.id, role: "ta" } | ||
expect(response).to have_http_status(:not_found) | ||
expect(JSON.parse(response.body)["error"]).to eq("The course does not exist.") | ||
end | ||
|
||
it "returns an error if user is not existed in the users table" do | ||
post :add_user, params: { course_id: test_course.id, user_id: 123456, role: "ta" } | ||
expect(response).to have_http_status(:not_found) | ||
expect(JSON.parse(response.body)["error"]).to eq("The user does not exist.") | ||
end | ||
|
||
it "returns an error if the user is already associated with the course" do | ||
post :add_user, params: { course_id: test_course.id, user_id: test_user.id, role: "student" } | ||
post :add_user, params: { course_id: test_course.id, user_id: test_user.id, role: "student" } | ||
expect(response).to have_http_status(:unprocessable_entity) | ||
expect(JSON.parse(response.body)["error"]).to eq("The user is already added to the course.") | ||
end | ||
end | ||
end | ||
end | ||
|