|
17 | 17 | source_title: "", |
18 | 18 | requester_name: "GDS Content Designer", |
19 | 19 | requester_email: "gds-content-designer@example.com", |
20 | | - current_content: "<p>Test HTML</p>", |
21 | | - previous_content: "", |
| 20 | + current_content: { |
| 21 | + "heading": "Some title words", |
| 22 | + "body": "Many lines of data for the content. Many changes that need fact checking", |
| 23 | + }, |
| 24 | + previous_content: {}, |
22 | 25 | deadline: 1.week.from_now.iso8601, |
23 | 26 | recipients: ["recipient1@example.com", "recipient2@example.com"], |
24 | 27 | } |
|
29 | 32 | expect { |
30 | 33 | post "/api/requests", params: valid_payload, as: :json |
31 | 34 | }.to change(Request, :count).by(1) |
32 | | - .and change(Collaboration, :count).by(2) |
| 35 | + .and change(Collaboration, :count).by(2) |
33 | 36 |
|
34 | 37 | expect(response).to have_http_status(:created) |
35 | 38 |
|
|
39 | 42 | request = Request.last |
40 | 43 | expect(request.source_app).to eq("Mainstream") |
41 | 44 | expect(request.source_id).to be_present |
42 | | - expect(request.current_content).to eq("<p>Test HTML</p>") |
| 45 | + expect(request.current_content["body"]).to eq("Many lines of data for the content. Many changes that need fact checking") |
43 | 46 | expect(request.status).to eq("new") |
44 | 47 | expect(request.requester_name).to eq("GDS Content Designer") |
45 | 48 | expect(request.requester_email).to eq("gds-content-designer@example.com") |
46 | 49 | end |
47 | 50 | end |
48 | 51 |
|
49 | | - context "with invalid payload" do |
| 52 | + context "with an invalid payload" do |
| 53 | + let(:base_payload) do |
| 54 | + { |
| 55 | + source_app: "Mainstream", |
| 56 | + source_id: SecureRandom.uuid, |
| 57 | + requester_name: "GDS Content Designer", |
| 58 | + requester_email: "gds-content-designer@example.com", |
| 59 | + current_content: dynamic_current_content, |
| 60 | + previous_content: {}, |
| 61 | + deadline: 1.week.from_now.iso8601, |
| 62 | + recipients: ["recipient1@example.com", "recipient2@example.com"], |
| 63 | + } |
| 64 | + end |
| 65 | + |
50 | 66 | it "returns errors for missing required fields" do |
51 | | - invalid_payload = { requester_name: "Alice", recipients: ["recipient1@example.com", "recipient2@example.com"] } |
| 67 | + payload_missing_required_fields = { requester_name: "Alice", |
| 68 | + recipients: ["recipient1@example.com", "recipient2@example.com"] } |
52 | 69 |
|
53 | | - post "/api/requests", params: invalid_payload, as: :json |
| 70 | + expect { |
| 71 | + post "/api/requests", params: payload_missing_required_fields, as: :json |
| 72 | + }.to change(Request, :count).by(0) |
| 73 | + .and change(Collaboration, :count).by(0) |
54 | 74 |
|
55 | | - expect(response).to have_http_status(:bad_request) |
| 75 | + expect(response).to have_http_status(:unprocessable_content) |
56 | 76 | json = JSON.parse(response.body) |
57 | 77 | expect(json["errors"]).to include( |
58 | 78 | "Source can't be blank", |
| 79 | + "Source app can't be blank", |
59 | 80 | "Requester email can't be blank", |
| 81 | + "Current content can't be blank", |
| 82 | + "Deadline can't be blank", |
60 | 83 | ) |
61 | 84 | end |
62 | | - end |
63 | 85 |
|
64 | | - context "without recipients" do |
65 | | - it "returns a 400 error" do |
66 | | - payload = valid_payload |
67 | | - payload.delete(:recipients) |
| 86 | + context "if current_content value is not a string" do |
| 87 | + let(:dynamic_current_content) do |
| 88 | + { |
| 89 | + "normal_field" => "This should pass", |
| 90 | + "bad_number_field" => 123, |
| 91 | + } |
| 92 | + end |
| 93 | + |
| 94 | + it "returns an error" do |
| 95 | + post "/api/requests", params: base_payload, as: :json |
| 96 | + |
| 97 | + expect(response).to have_http_status(:unprocessable_content) |
| 98 | + json = JSON.parse(response.body) |
| 99 | + expect(json["errors"]).to include("Current content value for bad_number_field must be a string") |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + context "if current_content contains nested data" do |
| 104 | + let(:dynamic_current_content) do |
| 105 | + { |
| 106 | + "normal_field" => "This should pass", |
| 107 | + "sneaky_nested_hash" => { "naughty" => "This should fail" }, |
| 108 | + } |
| 109 | + end |
| 110 | + |
| 111 | + it "returns an error" do |
| 112 | + expect { |
| 113 | + post "/api/requests", params: base_payload, as: :json |
| 114 | + }.to change(Request, :count).by(0) |
| 115 | + .and change(Collaboration, :count).by(0) |
| 116 | + expect(response).to have_http_status(:unprocessable_content) |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + context "without recipients" do |
| 121 | + it "returns a 400 error" do |
| 122 | + payload = valid_payload |
| 123 | + payload.delete(:recipients) |
68 | 124 |
|
69 | | - post "/api/requests", params: payload, as: :json |
| 125 | + post "/api/requests", params: payload, as: :json |
70 | 126 |
|
71 | | - expect(response).to have_http_status(:bad_request) |
72 | | - expect(JSON.parse(response.body)["errors"]) |
73 | | - .to include("At least one recipient email is required") |
| 127 | + expect(response).to have_http_status(:bad_request) |
| 128 | + expect(JSON.parse(response.body)["errors"]) |
| 129 | + .to include("At least one recipient email is required") |
| 130 | + end |
74 | 131 | end |
75 | 132 | end |
76 | 133 | end |
0 commit comments