Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Test #41

Open
tomsmith8 opened this issue Nov 12, 2024 · 0 comments
Open

Test: Test #41

tomsmith8 opened this issue Nov 12, 2024 · 0 comments

Comments

@tomsmith8
Copy link
Owner

Coverage for: Test


Stakwork Run


Unit Test Code


require 'rails_helper'

RSpec.describe TodoListsController, type: :controller do
describe 'POST #create' do
  let(:valid_attributes) { { title: 'Valid Title', description: 'Valid Description' } }
  let(:invalid_attributes) { { title: '', description: '' } }
  let(:min_required_attributes) { { title: 'Title' } }
  let(:max_length_attributes) { { title: 'T' * 255, description: 'D' * 1000 } }
  let(:null_values_attributes) { { title: nil, description: nil } }
  let(:invalid_data_types_attributes) { { title: 123, description: 456 } }
  let(:large_number_of_entries) { { title: 'Title', description: 'D' * 10000 } }
  let(:special_characters_attributes) { { title: '!@#$%^&*()', description: '<script>alert("test")</script>' } }
  let(:duplicate_attributes) { { title: 'Duplicate Title', description: 'Duplicate Description' } }
  let(:empty_strings_attributes) { { title: '', description: '' } }
  let(:html_injection_attributes) { { title: '<b>Bold Title</b>', description: '<script>alert("test")</script>' } }

  before do
    # Create a todo list to test duplicate entries
    TodoList.create!(duplicate_attributes)
  end

  context 'with valid HTML request' do
    it 'creates a todo list and redirects to the show page' do
      post :create, params: { todo_list: valid_attributes }, format: :html
      expect(response).to redirect_to(todo_list_path(assigns(:todo_list)))
      expect(flash[:success]).to eq('TodoList is successfully created')
    end
  end

  context 'with valid JSON request' do
    it 'creates a todo list and returns JSON response' do
      post :create, params: { todo_list: valid_attributes }, format: :json
      expect(response).to have_http_status(:ok)
      expect(JSON.parse(response.body)['message']).to eq('successfully created a todo list!')
    end
  end

  context 'with minimum required fields' do
    it 'creates a todo list successfully' do
      post :create, params: { todo_list: min_required_attributes }, format: :html
      expect(response).to redirect_to(todo_list_path(assigns(:todo_list)))
    end
  end

  context 'with maximum field lengths' do
    it 'creates a todo list successfully if within limits' do
      post :create, params: { todo_list: max_length_attributes }, format: :html
      expect(response).to redirect_to(todo_list_path(assigns(:todo_list)))
    end
  end

  context 'with invalid HTML request' do
    it 'does not create a todo list and renders new template' do
      post :create, params: { todo_list: invalid_attributes }, format: :html
      expect(response).to render_template(:new)
      expect(flash[:error]).to be_present
    end
  end

  context 'with invalid JSON request' do
    it 'does not create a todo list and returns JSON errors' do
      post :create, params: { todo_list: invalid_attributes }, format: :json
      expect(response).to have_http_status(:unprocessable_entity)
      expect(JSON.parse(response.body)['errors']).to be_present
    end
  end

  context 'with null values' do
    it 'does not create a todo list and returns errors' do
      post :create, params: { todo_list: null_values_attributes }, format: :json
      expect(response).to have_http_status(:unprocessable_entity)
      expect(JSON.parse(response.body)['errors']).to be_present
    end
  end

  context 'with invalid data types' do
    it 'does not create a todo list and returns errors' do
      post :create, params: { todo_list: invalid_data_types_attributes }, format: :json
      expect(response).to have_http_status(:unprocessable_entity)
      expect(JSON.parse(response.body)['errors']).to be_present
    end
  end

  context 'with large number of entries' do
    it 'creates a todo list successfully' do
      post :create, params: { todo_list: large_number_of_entries }, format: :html
      expect(response).to redirect_to(todo_list_path(assigns(:todo_list)))
    end
  end

  context 'with special characters' do
    it 'creates a todo list successfully and handles special characters' do
      post :create, params: { todo_list: special_characters_attributes }, format: :html
      expect(response).to redirect_to(todo_list_path(assigns(:todo_list)))
    end
  end

  context 'with duplicate entries' do
    it 'does not create a todo list and returns duplicate error' do
      post :create, params: { todo_list: duplicate_attributes }, format: :json
      expect(response).to have_http_status(:unprocessable_entity)
      expect(JSON.parse(response.body)['errors']).to include('Title has already been taken')
    end
  end

  context 'with empty strings' do
    it 'does not create a todo list and returns errors' do
      post :create, params: { todo_list: empty_strings_attributes }, format: :json
      expect(response).to have_http_status(:unprocessable_entity)
      expect(JSON.parse(response.body)['errors']).to be_present
    end
  end

  context 'with HTML/script injection' do
    it 'creates a todo list safely and sanitizes input' do
      post :create, params: { todo_list: html_injection_attributes }, format: :html
      expect(response).to redirect_to(todo_list_path(assigns(:todo_list)))
      # Additional checks for sanitization can be added here
    end
  end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant