|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +class OverseerImageApiTest < ActiveSupport::TestCase |
| 4 | + include Rack::Test::Methods |
| 5 | + include TestHelpers::AuthHelper |
| 6 | + include TestHelpers::JsonHelper |
| 7 | + include TestHelpers::OverseerTestHelper |
| 8 | + |
| 9 | + def app |
| 10 | + Rails.application |
| 11 | + end |
| 12 | + |
| 13 | + def setup |
| 14 | + setup_overseer_enabled |
| 15 | + end |
| 16 | + |
| 17 | + def test_get_all_overseer_images |
| 18 | + FactoryBot.create_list(:overseer_image, 5) |
| 19 | + expected_data = OverseerImage.all |
| 20 | + |
| 21 | + admin = FactoryBot.create(:user, :admin) |
| 22 | + add_auth_header_for(user: admin) |
| 23 | + |
| 24 | + get '/api/admin/overseer_images' |
| 25 | + |
| 26 | + assert_equal 200, last_response.status |
| 27 | + assert_equal expected_data.count, last_response_body.count, last_response_body |
| 28 | + |
| 29 | + response_keys = %w[name tag pulled_image_text pulled_image_status last_pulled_date] |
| 30 | + |
| 31 | + last_response_body.each do |data| |
| 32 | + expected_data = OverseerImage.find(data['id']) |
| 33 | + assert_json_matches_model(expected_data, data, response_keys) |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + def test_get_all_overseer_images_for_convenor |
| 38 | + FactoryBot.create_list(:overseer_image, 5) |
| 39 | + expected_data = OverseerImage.all |
| 40 | + convenor = FactoryBot.create(:user, :convenor) |
| 41 | + add_auth_header_for(user: convenor) |
| 42 | + |
| 43 | + get '/api/admin/overseer_images' |
| 44 | + |
| 45 | + assert_equal 200, last_response.status |
| 46 | + assert_equal expected_data.count, last_response_body.count, last_response_body |
| 47 | + end |
| 48 | + |
| 49 | + def test_no_get_for_students_or_tutors |
| 50 | + FactoryBot.create_list(:overseer_image, 5) |
| 51 | + expected_data = OverseerImage.all |
| 52 | + |
| 53 | + student = FactoryBot.create(:user, :student) |
| 54 | + add_auth_header_for(user: student) |
| 55 | + |
| 56 | + get '/api/admin/overseer_images' |
| 57 | + |
| 58 | + assert_equal 403, last_response.status |
| 59 | + |
| 60 | + tutor = FactoryBot.create(:user, :tutor) |
| 61 | + add_auth_header_for(user: tutor) |
| 62 | + |
| 63 | + get '/api/admin/overseer_images' |
| 64 | + |
| 65 | + assert_equal 403, last_response.status |
| 66 | + end |
| 67 | + |
| 68 | + def test_get_single_overseer_image |
| 69 | + overseer_image = FactoryBot.create(:overseer_image) |
| 70 | + |
| 71 | + admin = FactoryBot.create(:user, :admin) |
| 72 | + add_auth_header_for(user: admin) |
| 73 | + |
| 74 | + get "/api/admin/overseer_images/#{overseer_image.id}" |
| 75 | + |
| 76 | + assert_equal 200, last_response.status |
| 77 | + |
| 78 | + response_keys = %w[name tag pulled_image_text pulled_image_status last_pulled_date] |
| 79 | + assert_json_matches_model(overseer_image, last_response_body, response_keys) |
| 80 | + end |
| 81 | + |
| 82 | + def test_get_single_overseer_image_for_convenor |
| 83 | + overseer_image = FactoryBot.create(:overseer_image) |
| 84 | + |
| 85 | + convenor = FactoryBot.create(:user, :convenor) |
| 86 | + add_auth_header_for(user: convenor) |
| 87 | + |
| 88 | + get "/api/admin/overseer_images/#{overseer_image.id}" |
| 89 | + |
| 90 | + assert_equal 200, last_response.status |
| 91 | + |
| 92 | + response_keys = %w[name tag pulled_image_text pulled_image_status last_pulled_date] |
| 93 | + assert_json_matches_model(overseer_image, last_response_body, response_keys) |
| 94 | + end |
| 95 | + |
| 96 | + def test_no_get_single_overseer_image_for_students_or_tutors |
| 97 | + overseer_image = FactoryBot.create(:overseer_image) |
| 98 | + |
| 99 | + student = FactoryBot.create(:user, :student) |
| 100 | + add_auth_header_for(user: student) |
| 101 | + |
| 102 | + get "/api/admin/overseer_images/#{overseer_image.id}" |
| 103 | + |
| 104 | + assert_equal 403, last_response.status |
| 105 | + |
| 106 | + tutor = FactoryBot.create(:user, :tutor) |
| 107 | + |
| 108 | + add_auth_header_for(user: tutor) |
| 109 | + |
| 110 | + get "/api/admin/overseer_images/#{overseer_image.id}" |
| 111 | + |
| 112 | + assert_equal 403, last_response.status |
| 113 | + end |
| 114 | + |
| 115 | + # POST tests |
| 116 | + # 1: Admin can create a new overseer image |
| 117 | + def test_admin_can_post_overseer_image |
| 118 | + # Admin user |
| 119 | + admin = FactoryBot.create(:user, :admin) |
| 120 | + |
| 121 | + # the number of images before post |
| 122 | + no_images = OverseerImage.count |
| 123 | + |
| 124 | + # the data that we want to post/create |
| 125 | + data_to_post = { |
| 126 | + overseer_image: FactoryBot.build(:overseer_image) |
| 127 | + } |
| 128 | + |
| 129 | + # auth_token and username added to header |
| 130 | + add_auth_header_for(user: admin) |
| 131 | + |
| 132 | + # perform the POST |
| 133 | + post_json '/api/admin/overseer_images', data_to_post |
| 134 | + |
| 135 | + # check if the request get through |
| 136 | + assert_equal 201, last_response.status, "Failed to add image: #{data_to_post}" |
| 137 | + |
| 138 | + # check if the details posted match as expected |
| 139 | + response_keys = %w[name tag pulled_image_text pulled_image_status last_pulled_date] |
| 140 | + overseer_image = OverseerImage.find(last_response_body['id']) |
| 141 | + assert_json_matches_model(overseer_image, last_response_body, response_keys) |
| 142 | + |
| 143 | + assert_nil overseer_image.pulled_image_text |
| 144 | + assert_nil overseer_image.pulled_image_status |
| 145 | + |
| 146 | + # check if the details in the newly created match as pre-set data |
| 147 | + assert_equal data_to_post[:overseer_image]['name'], overseer_image.name |
| 148 | + assert_equal data_to_post[:overseer_image]['tag'], overseer_image.tag |
| 149 | + |
| 150 | + # check if one more image is created |
| 151 | + assert_equal no_images + 1, OverseerImage.count |
| 152 | + end |
| 153 | + |
| 154 | + # 2: Convenor cannot create a new overseer image |
| 155 | + def test_convenor_and_student_cannot_post_overseer_image |
| 156 | + # Convenor user |
| 157 | + convenor = FactoryBot.create(:user, :convenor) |
| 158 | + student = FactoryBot.create(:user, :student) |
| 159 | + |
| 160 | + # the number of teaching period before post |
| 161 | + overseer_image_type = OverseerImage.count |
| 162 | + |
| 163 | + # the data that we want to post/create |
| 164 | + data_to_post = { |
| 165 | + overseer_image: FactoryBot.build(:overseer_image) |
| 166 | + } |
| 167 | + |
| 168 | + # auth_token and username added to header |
| 169 | + add_auth_header_for(user: convenor) |
| 170 | + |
| 171 | + # perform the POST |
| 172 | + post_json '/api/admin/overseer_images', data_to_post |
| 173 | + |
| 174 | + # check if the request get through |
| 175 | + assert_equal 403, last_response.status |
| 176 | + |
| 177 | + # auth_token and username added to header |
| 178 | + add_auth_header_for(user: student) |
| 179 | + |
| 180 | + # perform the POST |
| 181 | + post_json '/api/admin/overseer_images', data_to_post |
| 182 | + |
| 183 | + # check if the request get through |
| 184 | + assert_equal 403, last_response.status |
| 185 | + |
| 186 | + # check if no more images is created |
| 187 | + assert_equal overseer_image_type, OverseerImage.count |
| 188 | + end |
| 189 | + |
| 190 | + # PUT tests |
| 191 | + # 1: Admin can replace an image |
| 192 | + def test_admin_can_put_overseer_image |
| 193 | + # Admin user |
| 194 | + admin = FactoryBot.create(:user, :admin) |
| 195 | + |
| 196 | + # The overseer image to be replaced |
| 197 | + overseer_image = FactoryBot.create(:overseer_image) |
| 198 | + |
| 199 | + # Data to replace |
| 200 | + data_to_put = { |
| 201 | + overseer_image: FactoryBot.build(:overseer_image) |
| 202 | + } |
| 203 | + |
| 204 | + # auth_token and username added to header |
| 205 | + add_auth_header_for(user: admin) |
| 206 | + |
| 207 | + # Update overseer_image with data_to_put |
| 208 | + put_json "/api/admin/overseer_images/#{overseer_image.id}", data_to_put |
| 209 | + |
| 210 | + # check if the request get through |
| 211 | + assert_equal 200, last_response.status, "Failed to update image: #{data_to_put} for #{overseer_image.inspect}" |
| 212 | + |
| 213 | + # check if the details posted match as expected |
| 214 | + response_keys = %w[name tag pulled_image_text pulled_image_status last_pulled_date] |
| 215 | + overseer_image_updated = overseer_image.reload |
| 216 | + assert_json_matches_model(overseer_image_updated, last_response_body, response_keys) |
| 217 | + |
| 218 | + # check if the details in the replaced teaching period match as data set to replace |
| 219 | + assert_equal data_to_put[:overseer_image]['name'], overseer_image_updated.name |
| 220 | + assert_equal data_to_put[:overseer_image]['tag'], overseer_image_updated.tag |
| 221 | + |
| 222 | + # Check other attributes are cleared |
| 223 | + assert_nil overseer_image_updated.pulled_image_text |
| 224 | + assert_nil overseer_image_updated.pulled_image_status |
| 225 | + end |
| 226 | + |
| 227 | + # 2: Convenor cannot replace an overseer image |
| 228 | + def test_non_admin_cannot_put_overseer_images |
| 229 | + # Convenor user |
| 230 | + convenor = FactoryBot.create(:user, :convenor) |
| 231 | + tutor = FactoryBot.create(:user, :tutor) |
| 232 | + student = FactoryBot.create(:user, :student) |
| 233 | + auditor = FactoryBot.create(:user, :auditor) |
| 234 | + |
| 235 | + users_to_test = [convenor, tutor, student, auditor] |
| 236 | + |
| 237 | + # The overseer image to be replaced |
| 238 | + overseer_image = FactoryBot.create(:overseer_image) |
| 239 | + |
| 240 | + # Data to replace |
| 241 | + data_to_put = { |
| 242 | + overseer_image: FactoryBot.build(:overseer_image) |
| 243 | + } |
| 244 | + |
| 245 | + users_to_test.each do |user| |
| 246 | + # auth_token and username added to header |
| 247 | + add_auth_header_for(user: user) |
| 248 | + |
| 249 | + # Update overseer_image with data_to_put |
| 250 | + put_json "/api/admin/overseer_images/#{overseer_image.id}", data_to_put |
| 251 | + |
| 252 | + # check if the request get through |
| 253 | + assert_equal 403, last_response.status, "User: #{user.role} updated overseer image" |
| 254 | + end |
| 255 | + end |
| 256 | + |
| 257 | + def test_delete_overseer_image |
| 258 | + # Create a overseer image |
| 259 | + overseer_image = FactoryBot.create(:overseer_image) |
| 260 | + |
| 261 | + # number of overseer image before delete |
| 262 | + number_of_images = OverseerImage.count |
| 263 | + |
| 264 | + # auth_token and username added to header |
| 265 | + admin = FactoryBot.create(:user, :admin) |
| 266 | + add_auth_header_for(user: admin) |
| 267 | + |
| 268 | + # perform the delete |
| 269 | + delete_json "/api/admin/overseer_images/#{overseer_image.id}" |
| 270 | + |
| 271 | + # Check if the delete get through |
| 272 | + assert_equal 200, last_response.status |
| 273 | + |
| 274 | + # Check delete if success |
| 275 | + assert_equal OverseerImage.count, number_of_images - 1 |
| 276 | + |
| 277 | + # Check that you can't find the deleted id |
| 278 | + assert_not OverseerImage.exists?(overseer_image.id) |
| 279 | + end |
| 280 | + |
| 281 | + def test_non_admin_cannot_delete_overseer_image |
| 282 | + # A user with student role which does not have permision to delete a overseer image |
| 283 | + users = [ |
| 284 | + FactoryBot.create(:user, :student), |
| 285 | + FactoryBot.create(:user, :tutor), |
| 286 | + FactoryBot.create(:user, :convenor), |
| 287 | + FactoryBot.create(:user, :auditor) |
| 288 | + ] |
| 289 | + |
| 290 | + # create a overseer image to delete |
| 291 | + overseer_image = FactoryBot.create (:overseer_image) |
| 292 | + |
| 293 | + # number of overseer image before delete |
| 294 | + number_of_images = OverseerImage.count |
| 295 | + |
| 296 | + users.each do |user| |
| 297 | + # auth_token and username added to header |
| 298 | + add_auth_header_for(user: user) |
| 299 | + |
| 300 | + # perform the delete |
| 301 | + delete_json "/api/admin/overseer_images/#{overseer_image.id}" |
| 302 | + |
| 303 | + # check if the delete does not get through |
| 304 | + assert_equal 403, last_response.status, "User: #{user.role} deleted overseer image" |
| 305 | + |
| 306 | + # check if the number of ativity_type is still the same |
| 307 | + assert_equal OverseerImage.count, number_of_images |
| 308 | + end |
| 309 | + |
| 310 | + # Check that you still can find the deleted id |
| 311 | + assert OverseerImage.exists?(overseer_image.id) |
| 312 | + end |
| 313 | + |
| 314 | + def test_delete_overseer_image_with_associated_units |
| 315 | + unit = FactoryBot.create(:unit, with_students: false) |
| 316 | + overseer_image = FactoryBot.create(:overseer_image, units: [unit]) |
| 317 | + unit.update(overseer_image: overseer_image) |
| 318 | + |
| 319 | + # number of overseer image before delete |
| 320 | + number_of_images = OverseerImage.count |
| 321 | + |
| 322 | + # auth_token and username added to header |
| 323 | + admin = FactoryBot.create(:user, :admin) |
| 324 | + add_auth_header_for(user: admin) |
| 325 | + |
| 326 | + # perform the delete |
| 327 | + delete_json "/api/admin/overseer_images/#{overseer_image.id}" |
| 328 | + |
| 329 | + # Check if the delete get through |
| 330 | + assert_equal 403, last_response.status |
| 331 | + |
| 332 | + # Check delete if success |
| 333 | + assert_equal OverseerImage.count, number_of_images |
| 334 | + |
| 335 | + # Check that you can't find the deleted id |
| 336 | + assert OverseerImage.exists?(overseer_image.id) |
| 337 | + end |
| 338 | + |
| 339 | + def test_delete_overseer_image_with_associated_task_definitions |
| 340 | + unit = FactoryBot.create(:unit, with_students: false) |
| 341 | + overseer_image = FactoryBot.create(:overseer_image, units: [unit]) |
| 342 | + unit.task_definitions.first.update(overseer_image: overseer_image) |
| 343 | + |
| 344 | + # number of overseer image before delete |
| 345 | + number_of_images = OverseerImage.count |
| 346 | + |
| 347 | + # auth_token and username added to header |
| 348 | + admin = FactoryBot.create(:user, :admin) |
| 349 | + add_auth_header_for(user: admin) |
| 350 | + |
| 351 | + # perform the delete |
| 352 | + delete_json "/api/admin/overseer_images/#{overseer_image.id}" |
| 353 | + |
| 354 | + # Check if the delete get through |
| 355 | + assert_equal 403, last_response.status |
| 356 | + |
| 357 | + # Check delete if success |
| 358 | + assert_equal OverseerImage.count, number_of_images |
| 359 | + |
| 360 | + # Check that you can't find the deleted id |
| 361 | + assert OverseerImage.exists?(overseer_image.id) |
| 362 | + end |
| 363 | +end |
0 commit comments