20300: Fix key order sensitivity in test, part 1.
[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
35     # (Temporary) sort the hash keys so we can diff JSON below.
36     canonical["resources"] = Hash[canonical["resources"].to_a.sort]
37     canonical["schemas"] = Hash[canonical["schemas"].to_a.sort]
38     actual_json = JSON.pretty_generate(canonical)
39
40     # Currently the Python SDK is the only component using this copy of the
41     # discovery document, and storing it with the source simplifies the build
42     # process, so it lives there. If another component wants to use it later,
43     # we might consider moving it to a more general subdirectory, but then the
44     # Python build process will need to be extended to accommodate that.
45     src_path = Rails.root.join("../../sdk/python/arvados-v1-discovery.json")
46     begin
47       expected_json = File.open(src_path) { |f| f.read }
48       # (Temporary) sort the hash keys so we can diff JSON below.
49       j = JSON.parse(expected_json)
50       j["resources"] = Hash[j["resources"].to_a.sort]
51       j["schemas"] = Hash[j["schemas"].to_a.sort]
52       expected_json = JSON.pretty_generate(j)
53     rescue Errno::ENOENT
54       expected_json = "(#{src_path} not found)"
55     end
56
57     out_path = Rails.root.join("tmp", "test-arvados-v1-discovery.json")
58     if expected_json != actual_json
59       File.open(out_path, "w") { |f| f.write(actual_json) }
60     end
61     assert_equal(expected_json, actual_json, [
62                    "#{src_path} did not match the live discovery document",
63                    "Current live version saved to #{out_path}",
64                    "Commit that to #{src_path} to regenerate documentation",
65                  ].join(". "))
66   end
67 end