Merge branch 'main' into 21720-material-ui-upgrade
[arvados.git] / services / api / test / integration / discovery_document_test.rb
1 # Copyright (C) The Arvados Authors. All rights reserved.
2 #
3 # SPDX-License-Identifier: AGPL-3.0
4
5 require 'test_helper'
6
7 class DiscoveryDocumentTest < ActionDispatch::IntegrationTest
8   CANONICAL_FIELDS = [
9     "auth",
10     "basePath",
11     "batchPath",
12     "description",
13     "discoveryVersion",
14     "documentationLink",
15     "id",
16     "kind",
17     "name",
18     "parameters",
19     "protocol",
20     "resources",
21     "revision",
22     "schemas",
23     "servicePath",
24     "title",
25     "version",
26   ]
27
28   test "canonical discovery document is saved to checkout" do
29     get "/discovery/v1/apis/arvados/v1/rest"
30     assert_response :success
31     canonical = Hash[CANONICAL_FIELDS.map { |key| [key, json_response[key]] }]
32     missing = canonical.select { |key| canonical[key].nil? }
33     assert(missing.empty?, "discovery document missing required fields")
34     actual_json = JSON.pretty_generate(canonical)
35
36     # Currently the Python SDK is the only component using this copy of the
37     # discovery document, and storing it with the source simplifies the build
38     # process, so it lives there. If another component wants to use it later,
39     # we might consider moving it to a more general subdirectory, but then the
40     # Python build process will need to be extended to accommodate that.
41     src_path = Rails.root.join("../../sdk/python/arvados-v1-discovery.json")
42     begin
43       expected_json = File.open(src_path) { |f| f.read }
44     rescue Errno::ENOENT
45       expected_json = "(#{src_path} not found)"
46     end
47
48     out_path = Rails.root.join("tmp", "test-arvados-v1-discovery.json")
49     if expected_json != actual_json
50       File.open(out_path, "w") { |f| f.write(actual_json) }
51     end
52     assert_equal(expected_json, actual_json,
53                  "Live discovery document did not match the expected version (#{src_path}). " +
54                  "If the live version is correct, copy it to the git working directory by running:\n" +
55                  "cp #{out_path} #{src_path}\n")
56   end
57 end