|
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +RSpec.describe "/resources", type: :request do |
| 4 | + let(:user) { create(:user) } |
| 5 | + let(:windows_type) { create(:windows_type) } |
| 6 | + let(:project) { create(:project) } |
| 7 | + |
| 8 | + let(:valid_attributes) do |
| 9 | + { |
| 10 | + title: "Helpful Resource", |
| 11 | + text: "This is a very helpful resource.", |
| 12 | + url: "https://www.example.com", |
| 13 | + inactive: false, |
| 14 | + kind: Resource::PUBLISHED_KINDS.first, |
| 15 | + windows_type_id: windows_type.id, |
| 16 | + user_id: user.id, |
| 17 | + } |
| 18 | + end |
| 19 | + |
| 20 | + let(:invalid_attributes) do |
| 21 | + { |
| 22 | + title: nil, |
| 23 | + text: "", |
| 24 | + kind: nil, |
| 25 | + user_id: user.id # REQUIRED |
| 26 | + } |
| 27 | + end |
| 28 | + |
| 29 | + before do |
| 30 | + sign_in user |
| 31 | + end |
| 32 | + |
| 33 | + describe "GET /index" do |
| 34 | + it "renders a successful response" do |
| 35 | + Resource.create! valid_attributes |
| 36 | + get resources_url |
| 37 | + |
| 38 | + expect(response).to have_http_status(:ok) |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + describe "GET /show" do |
| 43 | + context "when resource has NO external link" do |
| 44 | + let(:resource) do |
| 45 | + Resource.create!(valid_attributes.merge(url: nil)) |
| 46 | + end |
| 47 | + |
| 48 | + it "renders the show page" do |
| 49 | + get resource_url(resource) |
| 50 | + |
| 51 | + expect(response).to have_http_status(:ok) |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context "when resource HAS an external link" do |
| 56 | + let(:resource) do |
| 57 | + Resource.create!( |
| 58 | + valid_attributes.merge(url: "www.google.com") |
| 59 | + ) |
| 60 | + end |
| 61 | + |
| 62 | + it "redirects to the external URL" do |
| 63 | + get resource_url(resource) |
| 64 | + |
| 65 | + expect(response).to have_http_status(:found) |
| 66 | + expect(response).to redirect_to("https://www.google.com") |
| 67 | + end |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + describe "GET /new" do |
| 72 | + it "renders a successful response" do |
| 73 | + get new_resource_url |
| 74 | + |
| 75 | + expect(response).to have_http_status(:ok) |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + describe "GET /edit" do |
| 80 | + it "renders a successful response" do |
| 81 | + resource = Resource.create! valid_attributes |
| 82 | + get edit_resource_url(resource) |
| 83 | + |
| 84 | + expect(response).to have_http_status(:ok) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + describe "POST /create" do |
| 89 | + context "with valid parameters" do |
| 90 | + it "creates a new Resource" do |
| 91 | + expect { |
| 92 | + post resources_url, params: { resource: valid_attributes } |
| 93 | + }.to change(Resource, :count).by(1) |
| 94 | + end |
| 95 | + |
| 96 | + it "redirects to the resources index" do |
| 97 | + post resources_url, params: { resource: valid_attributes } |
| 98 | + |
| 99 | + expect(response).to redirect_to(resources_url) |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + context "with invalid parameters" do |
| 104 | + it "does not create a new Resource" do |
| 105 | + expect { |
| 106 | + post resources_url, params: { resource: invalid_attributes } |
| 107 | + }.not_to change(Resource, :count) |
| 108 | + end |
| 109 | + |
| 110 | + it "renders a response with 422 status" do |
| 111 | + post resources_url, params: { resource: invalid_attributes } |
| 112 | + |
| 113 | + expect(response).to have_http_status(:unprocessable_content) |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + describe "PATCH /update" do |
| 119 | + context "with valid parameters" do |
| 120 | + let(:new_attributes) do |
| 121 | + valid_attributes.merge(title: "Updated Resource Title") |
| 122 | + end |
| 123 | + |
| 124 | + it "updates the requested resource" do |
| 125 | + resource = Resource.create! valid_attributes |
| 126 | + patch resource_url(resource), params: { resource: new_attributes } |
| 127 | + |
| 128 | + resource.reload |
| 129 | + expect(resource.title).to eq("Updated Resource Title") |
| 130 | + end |
| 131 | + |
| 132 | + it "redirects to the resources index" do |
| 133 | + resource = Resource.create! valid_attributes |
| 134 | + patch resource_url(resource), params: { resource: new_attributes } |
| 135 | + |
| 136 | + expect(response).to redirect_to(resources_url) |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + context "with invalid parameters" do |
| 141 | + it "renders a response with 422 status" do |
| 142 | + resource = Resource.create! valid_attributes |
| 143 | + patch resource_url(resource), params: { resource: invalid_attributes } |
| 144 | + |
| 145 | + expect(response).to have_http_status(:unprocessable_content) |
| 146 | + end |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + describe "DELETE /destroy" do |
| 151 | + it "destroys the requested resource" do |
| 152 | + resource = Resource.create! valid_attributes |
| 153 | + |
| 154 | + expect { |
| 155 | + delete resource_url(resource) |
| 156 | + }.to change(Resource, :count).by(-1) |
| 157 | + end |
| 158 | + |
| 159 | + it "redirects to the resources list" do |
| 160 | + resource = Resource.create! valid_attributes |
| 161 | + delete resource_url(resource) |
| 162 | + |
| 163 | + expect(response).to redirect_to(resources_url) |
| 164 | + end |
| 165 | + end |
| 166 | +end |
0 commit comments