|
1 | 1 | RSpec.describe 'PxeServers API' do |
2 | | - let!(:pxe_server) { FactoryBot.create(:pxe_server) } |
| 2 | + let(:pxe_server) { FactoryBot.build(:pxe_server).tap { |x| x.update_authentication(:default => {:userid => 'foo', :password => 'bar'}) } } |
3 | 3 | let!(:pxe_image_1) { FactoryBot.create(:pxe_image, :pxe_server => pxe_server) } |
4 | 4 | let!(:pxe_image_2) { FactoryBot.create(:pxe_image, :pxe_server => pxe_server) } |
| 5 | + let!(:pxe_menu_1) { FactoryBot.create(:pxe_menu, :pxe_server => pxe_server) } |
5 | 6 |
|
6 | 7 | describe 'GET /api/pxe_servers' do |
7 | 8 | let(:url) { api_pxe_servers_url } |
|
90 | 91 | expect(response).to have_http_status(:forbidden) |
91 | 92 | end |
92 | 93 | end |
| 94 | + |
| 95 | + describe 'GET /api/pxe_servers/:id/pxe_menus' do |
| 96 | + let(:url) { "/api/pxe_servers/#{pxe_server.id}/pxe_menus" } |
| 97 | + |
| 98 | + it 'lists all pxe menus of a pxe server' do |
| 99 | + api_basic_authorize subcollection_action_identifier(:pxe_servers, :pxe_menus, :read, :get) |
| 100 | + get(url) |
| 101 | + expect_result_resources_to_include_hrefs( |
| 102 | + "resources", |
| 103 | + [ |
| 104 | + api_pxe_server_pxe_menu_url(nil, pxe_server, pxe_menu_1) |
| 105 | + ] |
| 106 | + ) |
| 107 | + expect(response).to have_http_status(:ok) |
| 108 | + end |
| 109 | + |
| 110 | + it 'forbids access without an appropriate role' do |
| 111 | + api_basic_authorize |
| 112 | + get(url) |
| 113 | + expect(response).to have_http_status(:forbidden) |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + describe 'GET /api/pxe_servers/:id/pxe_menus/:id' do |
| 118 | + let(:url) { "/api/pxe_servers/#{pxe_server.id}/pxe_menus/#{pxe_menu_1.id}" } |
| 119 | + |
| 120 | + it "will show a single pxe menu of a pxe server" do |
| 121 | + api_basic_authorize subcollection_action_identifier(:pxe_servers, :pxe_images, :read, :get) |
| 122 | + get(url) |
| 123 | + expect_result_to_match_hash( |
| 124 | + response.parsed_body, |
| 125 | + "href" => api_pxe_server_pxe_menu_url(nil, pxe_server, pxe_menu_1), |
| 126 | + "id" => pxe_menu_1.id.to_s, |
| 127 | + "pxe_server_id" => pxe_server.id.to_s, |
| 128 | + "file_name" => pxe_menu_1.file_name |
| 129 | + ) |
| 130 | + expect(response).to have_http_status(:ok) |
| 131 | + end |
| 132 | + |
| 133 | + it 'forbids access without an appropriate role' do |
| 134 | + api_basic_authorize |
| 135 | + get(url) |
| 136 | + expect(response).to have_http_status(:forbidden) |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + describe 'POST /api/pxe_servers/' do |
| 141 | + let(:url) { "/api/pxe_servers/" } |
| 142 | + |
| 143 | + it 'create new pxe server' do |
| 144 | + api_basic_authorize collection_action_identifier(:pxe_servers, :create, :post) |
| 145 | + post( |
| 146 | + url, |
| 147 | + :params => { |
| 148 | + :name => 'test server', |
| 149 | + :uri => 'bar://quax', |
| 150 | + :authentication => { |
| 151 | + :userid => 'foo', |
| 152 | + :password => 'bar' |
| 153 | + } |
| 154 | + } |
| 155 | + ) |
| 156 | + expect(response).to have_http_status(:ok) |
| 157 | + expect(response.parsed_body['results'].first['name']).to eq('test server') |
| 158 | + expect(response.parsed_body['results'].first['uri']).to eq('bar://quax') |
| 159 | + expect(response.parsed_body['results'].first['uri_prefix']).to eq('bar') |
| 160 | + end |
| 161 | + |
| 162 | + it 'create new nfs pxe server without authentication' do |
| 163 | + api_basic_authorize collection_action_identifier(:pxe_servers, :create, :post) |
| 164 | + post(url, :params => {:name => 'test server', :uri => 'nfs://quax'}) |
| 165 | + expect(response).to have_http_status(:ok) |
| 166 | + expect(response.parsed_body['results'].first['name']).to eq('test server') |
| 167 | + expect(response.parsed_body['results'].first['uri']).to eq('nfs://quax') |
| 168 | + expect(response.parsed_body['results'].first['uri_prefix']).to eq('nfs') |
| 169 | + end |
| 170 | + |
| 171 | + it 'create new pxe server with pxe menu' do |
| 172 | + api_basic_authorize collection_action_identifier(:pxe_servers, :create, :post) |
| 173 | + post(url, :params => {:name => 'foo', :uri => 'bar://quax', |
| 174 | + :pxe_menus => [{:file_name => 'menu_1'}, {:file_name => 'menu_2'}], |
| 175 | + :authentication => {:userid => 'foo', :password => 'bar' }}) |
| 176 | + expect(response).to have_http_status(:ok) |
| 177 | + expect(PxeServer.find(response.parsed_body['results'].first['id']).pxe_menus.first[:file_name]).to eq('menu_1') |
| 178 | + end |
| 179 | + |
| 180 | + it 'fail to create new non nfs pxe server without correct authentication' do |
| 181 | + api_basic_authorize collection_action_identifier(:pxe_servers, :create, :post) |
| 182 | + post(url, :params => {:name => 'test server', :uri => 'smb://quax', :authentication => {:userid => 'user'}}) |
| 183 | + expect(response).to have_http_status(:bad_request) |
| 184 | + end |
| 185 | + |
| 186 | + it 'will forbid create action withouth an appropriate authorization' do |
| 187 | + api_basic_authorize |
| 188 | + post(url, :params => {:name => 'foo', :uri => 'bar/quax', |
| 189 | + :pxe_menus => [{:file_name => 'menu_1'}, {:file_name => 'menu_2'}], |
| 190 | + :authentication => {:userid => 'foo', :password => 'bar' }}) |
| 191 | + expect(response).to have_http_status(:forbidden) |
| 192 | + end |
| 193 | + end |
| 194 | + |
| 195 | + describe 'update /api/pxe_servers/:id' do |
| 196 | + let(:url) { "/api/pxe_servers/#{pxe_server.id}" } |
| 197 | + |
| 198 | + it 'update pxe server' do |
| 199 | + api_basic_authorize collection_action_identifier(:pxe_servers, :edit, :patch) |
| 200 | + patch(url, :params => {:name => 'updated name', :uri => 'updated://url', :pxe_menus => [{:file_name => 'updated menu'}]}) |
| 201 | + expect(response).to have_http_status(:ok) |
| 202 | + expect(response.parsed_body['name']).to eq('updated name') |
| 203 | + expect(response.parsed_body['uri']).to eq('updated://url') |
| 204 | + expect(PxeServer.find(response.parsed_body['id']).pxe_menus.first[:file_name]).to eq('updated menu') |
| 205 | + end |
| 206 | + |
| 207 | + it 'will forbid update action withouth an appropriate authorization' do |
| 208 | + api_basic_authorize |
| 209 | + patch(url, :params => {:name => 'updated name', :uri => 'updated://url', :pxe_menus => [{:file_name => 'updated menu'}]}) |
| 210 | + expect(response).to have_http_status(:forbidden) |
| 211 | + end |
| 212 | + end |
| 213 | + |
| 214 | + describe 'delete /api/pxe_servers/:id' do |
| 215 | + let(:url) { "/api/pxe_servers/#{pxe_server.id}" } |
| 216 | + |
| 217 | + it 'delete pxe server' do |
| 218 | + api_basic_authorize collection_action_identifier(:pxe_servers, :delete, :delete) |
| 219 | + delete(url) |
| 220 | + expect(response).to have_http_status(:no_content) |
| 221 | + expect(PxeServer.exists?(pxe_server.id)).to be_falsey |
| 222 | + end |
| 223 | + |
| 224 | + it 'will forbid delete action withouth an appropriate authorization' do |
| 225 | + api_basic_authorize |
| 226 | + delete(url) |
| 227 | + expect(response).to have_http_status(:forbidden) |
| 228 | + end |
| 229 | + end |
93 | 230 | end |
0 commit comments