From f587dc3c3295ce61a16fde880eaae4fc16fb4029 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Mon, 9 Jan 2023 09:43:52 +0100 Subject: [PATCH] (test) Group --- test/integration/groups_test.rb | 76 +++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 test/integration/groups_test.rb diff --git a/test/integration/groups_test.rb b/test/integration/groups_test.rb new file mode 100644 index 000000000..77e85777c --- /dev/null +++ b/test/integration/groups_test.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +require 'test_helper' + +class GroupsTest < ActionDispatch::IntegrationTest + def setup + @admin = User.find_by(username: 'admin') + login_as(@admin, scope: :user) + end + + test 'create a group' do + post '/api/groups', + params: { + name: 'Strange people', + disabled: true + }.to_json, + headers: default_headers + + # Check response format & status + assert_equal 201, response.status, response.body + assert_equal Mime[:json], response.content_type + + # Check the correct group was created + res = json_response(response.body) + group = Group.where(id: res[:id]).first + assert_not_nil group, 'group was not created in database' + + assert_equal 'Strange people', res[:name] + assert_equal true, res[:disabled] + end + + test 'update a group' do + patch '/api/groups/1', + params: { + name: 'Normal people' + }.to_json, + headers: default_headers + + # Check response format & status + assert_equal 200, response.status, response.body + assert_equal Mime[:json], response.content_type + + # Check the group was updated + res = json_response(response.body) + assert_equal 1, res[:id] + assert_equal 'Normal people', res[:name] + end + + test 'list all groups' do + get '/api/groups' + + # Check response format & status + assert_equal 200, response.status, response.body + assert_equal Mime[:json], response.content_type + + # Check the list items are ok + groups = json_response(response.body) + assert_equal Group.count, groups.count + end + + test 'delete a group' do + group = Group.create!(name: 'delete me') + delete "/api/groups/#{group.id}" + assert_response :success + assert_empty response.body + assert_raise ActiveRecord::RecordNotFound do + group.reload + end + end + + test 'unable to delete a used group' do + delete '/api/groups/1' + assert_response :forbidden + assert_not_nil Group.find(1) + end +end