Below is a straight-forward expansion of your request spec so it now covers the four remaining RESTful actions: new, create, update and destroy.
I tried to keep the structure and naming identical to your existing “show” examples so the suite stays readable and consistent.
# spec/requests/cs/devices_spec.rb
require 'rails_helper'
RSpec.describe 'CS::Devices', type: :request do
# ---------------------------------------------------------------------------
# Helper to sign-in and hit an endpoint in one line
# ---------------------------------------------------------------------------
def auth_get(path, user, **args)
cookies['enectiva_jwt'] = sign_in(user)
get path, **args
end
def auth_post(path, user, **args)
cookies['enectiva_jwt'] = sign_in(user)
post path, **args
end
def auth_patch(path, user, **args)
cookies['enectiva_jwt'] = sign_in(user)
patch path, **args
end
def auth_delete(path, user, **args)
cookies['enectiva_jwt'] = sign_in(user)
delete path, **args
end
# ---------------------------------------------------------------------------
# SHOW (already present) – left untouched for reference
# ---------------------------------------------------------------------------
context 'show' do
context 'user with access to parent entity' do
it 'allows access' do
customer = create :customer
entity = create :entity, customer: customer
user = create :user, :guest, customer: customer, entity_ids: [entity.id]
device = create :device, entity: entity, customer: customer
auth_get "/cs/devices/#{device.id}", user
expect(response).to have_http_status(:ok)
end
end
context 'user without access to parent entity' do
it 'returns 404' do
customer = create :customer
entity = create :entity, customer: customer
user = create :user, :guest, customer: customer, entity_ids: [] # ← no access
device = create :device, entity: entity, customer: customer
auth_get "/cs/devices/#{device.id}", user
expect(response).to have_http_status(:not_found)
end
end
end
# ===========================================================================
# NEW
# ===========================================================================
context 'new' do
context 'user with access to parent entity' do
it 'renders the form' do
customer = create :customer
entity = create :entity, customer: customer
user = create :user, :guest, customer: customer, entity_ids: [entity.id]
auth_get "/cs/entities/#{entity.id}/devices/new", user # nested route
expect(response).to have_http_status(:ok)
end
end
context 'user without access to parent entity' do
it 'returns 404' do
customer = create :customer
entity = create :entity, customer: customer
user = create :user, :guest, customer: customer, entity_ids: [] # ← no access
auth_get "/cs/entities/#{entity.id}/devices/new", user
expect(response).to have_http_status(:not_found)
end
end
end
# ===========================================================================
# CREATE
# ===========================================================================
context 'create' do
let(:device_params) do
{
device: attributes_for(:device).merge(name: 'New Device')
}
end
context 'user with access to parent entity' do
it 'creates the device and redirects' do
customer = create :customer
entity = create :entity, customer: customer
user = create :user, :guest, customer: customer, entity_ids: [entity.id]
expect {
auth_post "/cs/entities/#{entity.id}/devices", user, params: device_params
}.to change(Device, :count).by(1)
expect(response).to redirect_to(cs_device_path(Device.last))
end
end
context 'user without access to parent entity' do
it 'does not create and returns 404' do
customer = create :customer
entity = create :entity, customer: customer
user = create :user, :guest, customer: customer, entity_ids: []
expect {
auth_post "/cs/entities/#{entity.id}/devices", user, params: device_params
}.not_to change(Device, :count)
expect(response).to have_http_status(:not_found)
end
end
end
# ===========================================================================
# UPDATE
# ===========================================================================
context 'update' do
let(:update_params) { { device: { name: 'Updated Name' } } }
context 'user with access to parent entity' do
it 'updates the record' do
customer = create :customer
entity = create :entity, customer: customer
device = create :device, name: 'Old Name', entity: entity, customer: customer
user = create :user, :guest, customer: customer, entity_ids: [entity.id]
auth_patch "/cs/devices/#{device.id}", user, params: update_params
expect(response).to redirect_to(cs_device_path(device))
expect(device.reload.name).to eq('Updated Name')
end
end
context 'user without access to parent entity' do
it 'does not update and returns 404' do
customer = create :customer
entity = create :entity, customer: customer
device = create :device, name: 'Untouched', entity: entity, customer: customer
user = create :user, :guest, customer: customer, entity_ids: []
auth_patch "/cs/devices/#{device.id}", user, params: update_params
expect(response).to have_http_status(:not_found)
expect(device.reload.name).to eq('Untouched')
end
end
end
# ===========================================================================
# DESTROY
# ===========================================================================
context 'destroy' do
context 'user with access to parent entity' do
it 'deletes the record' do
customer = create :customer
entity = create :entity, customer: customer
device = create :device, entity: entity, customer: customer
user = create :user, :guest, customer: customer, entity_ids: [entity.id]
expect {
auth_delete "/cs/devices/#{device.id}", user
}.to change(Device, :count).by(-1)
expect(response).to redirect_to(cs_devices_path)
end
end
context 'user without access to parent entity' do
it 'does not delete and returns 404' do
customer = create :customer
entity = create :entity, customer: customer
device = create :device, entity: entity, customer: customer
user = create :user, :guest, customer: customer, entity_ids: []
expect {
auth_delete "/cs/devices/#{device.id}", user
}.not_to change(Device, :count)
expect(response).to have_http_status(:not_found)
end
end
end
end
What was added / changed:
- Four new
context blocks (new, create, update, destroy).
- A small helper (
auth_get, auth_post, etc.) to DRY-up the sign-in boilerplate.
- Parameter hashes for create and update.
- Assertions for HTTP status codes, redirects, and DB persistence.
Feel free to tweak routes or param keys to match your actual controller implementation.