|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'Updating a project', type: :request do |
| 6 | + before do |
| 7 | + stub_hydra_public_api |
| 8 | + stub_user_info_api |
| 9 | + |
| 10 | + create(:component, project:, name: 'main', extension: 'py', content: 'print("hi")') |
| 11 | + end |
| 12 | + |
| 13 | + let(:headers) { { Authorization: UserProfileMock::TOKEN } } |
| 14 | + let!(:project) { create(:project, name: 'Test Project', user_id: owner_id) } |
| 15 | + let(:owner_index) { user_index_by_role('school-owner') } |
| 16 | + let(:owner_id) { user_id_by_index(owner_index) } |
| 17 | + |
| 18 | + let(:params) do |
| 19 | + { |
| 20 | + project: { |
| 21 | + name: 'New Name', |
| 22 | + components: [ |
| 23 | + { name: 'main', extension: 'py', content: 'print("hello")' } |
| 24 | + ] |
| 25 | + } |
| 26 | + } |
| 27 | + end |
| 28 | + |
| 29 | + it 'responds 200 OK' do |
| 30 | + put("/api/projects/#{project.id}", headers:, params:) |
| 31 | + expect(response).to have_http_status(:ok) |
| 32 | + end |
| 33 | + |
| 34 | + it 'responds with the project JSON' do |
| 35 | + put("/api/projects/#{project.id}", headers:, params:) |
| 36 | + data = JSON.parse(response.body, symbolize_names: true) |
| 37 | + |
| 38 | + expect(data[:name]).to eq('New Name') |
| 39 | + end |
| 40 | + |
| 41 | + it 'responds with the components JSON' do |
| 42 | + put("/api/projects/#{project.id}", headers:, params:) |
| 43 | + data = JSON.parse(response.body, symbolize_names: true) |
| 44 | + |
| 45 | + expect(data[:components].first[:content]).to eq('print("hello")') |
| 46 | + end |
| 47 | + |
| 48 | + it 'responds 422 Unprocessable Entity when params are invalid' do |
| 49 | + put("/api/projects/#{project.id}", headers:, params: { project: { components: [{ name: ' ' }] } }) |
| 50 | + expect(response).to have_http_status(:unprocessable_entity) |
| 51 | + end |
| 52 | + |
| 53 | + it 'responds 401 Unauthorized when no token is given' do |
| 54 | + put("/api/projects/#{project.id}", params:) |
| 55 | + expect(response).to have_http_status(:unauthorized) |
| 56 | + end |
| 57 | +end |
0 commit comments