Skip to content

Commit

Permalink
Update state to archived_with_draft when archived form is updated
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencdaly committed Mar 28, 2024
1 parent b39b1f5 commit 7c1f2b0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/controllers/api/v1/forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def update
# TODO: https://trello.com/c/dg9CFPgp/1503-user-triggers-state-change-from-live-to-livewithdraft
# Will not be needed when users can trigger this event themselves through the UI
form.create_draft_from_live_form! if form.live?
form.create_draft_from_archived_form! if form.archived?

render json: form.to_json, status: :ok
else
Expand Down
5 changes: 5 additions & 0 deletions spec/factories/forms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
after(:create, &:make_live!)
end

trait :archived do
live
after(:create, &:archive_live_form!)
end

trait :with_support do
support_email { Faker::Internet.email(domain: "example.gov.uk") }
support_phone { Faker::Lorem.paragraph(sentence_count: 2, supplemental: true, random_sentences_to_add: 4) }
Expand Down
31 changes: 24 additions & 7 deletions spec/requests/api/v1/forms_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@
end

describe "#update" do
let(:form) { create :form }

it "when no forms exists for an id, returns 404 an error" do
put form_path(id: 999), as: :json
expect(response).to have_http_status(:not_found)
Expand All @@ -223,22 +225,37 @@
end

it "when given an valid id and params, updates DB and returns 200" do
form1 = create :form
put form_path(form1), params: { submission_email: "[email protected]" }, as: :json
put form_path(form), params: { submission_email: "[email protected]" }, as: :json
expect(response).to have_http_status(:ok)
expect(response.headers["Content-Type"]).to eq("application/json")
expect(json_body).to include(submission_email: "[email protected]")
expect(form1.reload.submission_email).to eq("[email protected]")
expect(form.reload.submission_email).to eq("[email protected]")
end

it "ignores created_at" do
form1 = create :form
expect { put form_path(form1), params: { created_at: "2023-01-11T16:22:22.661+00:00" }, as: :json }.not_to(change { form1.reload.created_at })
expect { put form_path(form), params: { created_at: "2023-01-11T16:22:22.661+00:00" }, as: :json }.not_to(change { form.reload.created_at })
end

it "ignores updated_at" do
form1 = create :form
expect { put form_path(form1), params: { updated_at: "2023-01-11T16:22:22.661+00:00" }, as: :json }.not_to(change { form1.reload.updated_at })
expect { put form_path(form), params: { updated_at: "2023-01-11T16:22:22.661+00:00" }, as: :json }.not_to(change { form.reload.updated_at })
end

context "when form is live" do
let(:form) { create(:form, :live) }

it "updates form state to live_with_draft" do
put form_path(form), params: { submission_email: "[email protected]" }, as: :json
expect(form.reload.state).to eq("live_with_draft")
end
end

context "when form is archived" do
let(:form) { create(:form, :archived) }

it "updates form state to archived_with_draft" do
put form_path(form), params: { submission_email: "[email protected]" }, as: :json
expect(form.reload.state).to eq("archived_with_draft")
end
end
end

Expand Down

0 comments on commit 7c1f2b0

Please sign in to comment.