|
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +RSpec.describe "Training Resources", type: :request do |
| 4 | + describe "POST /training_resources" do |
| 5 | + let(:user) { create(:user) } |
| 6 | + let(:topic) { create(:topic) } |
| 7 | + let(:valid_attributes) { attributes_for(:training_resource, topic_id: topic.id) } |
| 8 | + let(:invalid_attributes) { { document: "", file_name_override: "", state: "", topic_id: "" } } |
| 9 | + |
| 10 | + before { sign_in(user) } |
| 11 | + |
| 12 | + context "with valid parameters" do |
| 13 | + it "creates a new TrainingResource" do |
| 14 | + expect { |
| 15 | + post training_resources_url, params: { training_resource: valid_attributes } |
| 16 | + }.to change(TrainingResource, :count).by(1) |
| 17 | + end |
| 18 | + |
| 19 | + it "redirects to the created training_resource" do |
| 20 | + post training_resources_url, params: { training_resource: valid_attributes } |
| 21 | + expect(response).to redirect_to(training_resource_url(TrainingResource.last)) |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + context "with invalid parameters" do |
| 26 | + it "does not create a new TrainingResource" do |
| 27 | + expect { |
| 28 | + post training_resources_url, params: { training_resource: invalid_attributes } |
| 29 | + }.to change(TrainingResource, :count).by(0) |
| 30 | + end |
| 31 | + |
| 32 | + it "renders a response with 422 status (i.e. to display the 'new' template)" do |
| 33 | + post training_resources_url, params: { training_resource: invalid_attributes } |
| 34 | + expect(response).to have_http_status(:unprocessable_entity) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + context "with file_name_override present" do |
| 39 | + it "does not create a new TrainingResource if there is already a preexistent entry with the same language" do |
| 40 | + preexistent_topic = create(:topic, language_id: topic.language_id) |
| 41 | + TrainingResource.create! valid_attributes |
| 42 | + |
| 43 | + post training_resources_url, params: { training_resource: valid_attributes.merge(topic_id: preexistent_topic.id) } |
| 44 | + |
| 45 | + expect(response).to have_http_status(:unprocessable_entity) |
| 46 | + end |
| 47 | + |
| 48 | + it "creates a new Training Resource if file_name_override has a different language" do |
| 49 | + preexistent_topic = create(:topic) |
| 50 | + TrainingResource.create! valid_attributes |
| 51 | + |
| 52 | + post training_resources_url, params: { training_resource: valid_attributes.merge(topic_id: preexistent_topic.id) } |
| 53 | + |
| 54 | + expect(response).to have_http_status(:redirect) |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | +end |
0 commit comments