# Copyright (C) The Arvados Authors. All rights reserved.
#
# SPDX-License-Identifier: AGPL-3.0

require 'test_helper'

class Arvados::V1::SchemaControllerTest < ActionController::TestCase

  setup do forget end
  teardown do forget end
  def forget
    AppVersion.forget
  end

  test "should get fresh discovery document" do
    MAX_SCHEMA_AGE = 60
    get :index
    assert_response :success
    discovery_doc = JSON.parse(@response.body)
    assert_equal 'discovery#restDescription', discovery_doc['kind']
    assert_equal(true,
                 Time.now - MAX_SCHEMA_AGE.seconds < discovery_doc['generatedAt'],
                 "discovery document was generated >#{MAX_SCHEMA_AGE}s ago")
  end

  test "discovery document fields" do
    get :index
    assert_response :success
    discovery_doc = JSON.parse(@response.body)
    assert_includes discovery_doc, 'defaultTrashLifetime'
    assert_equal discovery_doc['defaultTrashLifetime'], Rails.configuration.Collections.DefaultTrashLifetime
    assert_match(/^[0-9a-f]+(-modified)?$/, discovery_doc['source_version'])
    assert_match(/^[0-9a-f]+(-modified)?$/, discovery_doc['sourceVersion'])
    assert_equal discovery_doc['websocketUrl'], Rails.configuration.Services.Websocket.ExternalURL.to_s
    assert_equal discovery_doc['workbenchUrl'], Rails.configuration.Services.Workbench1.ExternalURL.to_s
    assert_equal('zzzzz', discovery_doc['uuidPrefix'])
  end

  test "discovery document overrides source_version & sourceVersion with config" do
    Rails.configuration.source_version = 'aaa888fff'
    get :index
    assert_response :success
    discovery_doc = JSON.parse(@response.body)
    # Key source_version will be replaced with sourceVersion
    assert_equal 'aaa888fff', discovery_doc['source_version']
    assert_equal 'aaa888fff', discovery_doc['sourceVersion']
  end

  ["unknown", "1.0.1-stable"].each do |pkg_version|
    test "packageVersion #{pkg_version} comes from AppVersion" do
      AppVersion.stubs(:package_version).returns(pkg_version)
      get :index
      assert_response :success
      assert_equal(pkg_version, json_response["packageVersion"])
    end
  end

  test "discovery document overrides packageVersion with config" do
    Rails.configuration.package_version = '1.0.0-stable'
    get :index
    assert_response :success
    discovery_doc = JSON.parse(@response.body)
    assert_equal '1.0.0-stable', discovery_doc['packageVersion']
  end

  test "empty disable_api_methods" do
    get :index
    assert_response :success
    discovery_doc = JSON.parse(@response.body)
    assert_equal('POST',
                 discovery_doc['resources']['collections']['methods']['create']['httpMethod'])
  end

  test "non-empty disable_api_methods" do
    Rails.configuration.API.DisabledAPIs = ConfigLoader.to_OrderedOptions(
      {'collections.create'=>{}, 'workflows.create'=>{}})
    get :index
    assert_response :success
    discovery_doc = JSON.parse(@response.body)
    ['collections', 'workflows'].each do |r|
      refute_includes(discovery_doc['resources'][r]['methods'].keys(), 'create')
    end
  end

  test "groups contents parameters" do
    get :index
    assert_response :success

    groups_methods = JSON.parse(@response.body)['resources']['groups']['methods']
    group_index_params = groups_methods['list']['parameters'].each_pair.to_a
    group_contents_params = groups_methods['contents']['parameters'].each_pair.to_a

    assert_equal(
      group_contents_params & group_index_params, group_index_params,
      "group contents methods does not take all the same parameters index does",
    )

    recursive_param = groups_methods['contents']['parameters']['recursive']
    assert_equal 'boolean', recursive_param['type']
    assert_equal false, recursive_param['required']
    assert_equal 'query', recursive_param['location']
  end

  test "collections index parameters" do
    get :index
    assert_response :success

    discovery_doc = JSON.parse(@response.body)

    workflows_index_params = discovery_doc['resources']['workflows']['methods']['list']['parameters']  # no changes from super
    coll_index_params = discovery_doc['resources']['collections']['methods']['list']['parameters']

    assert_equal (workflows_index_params.keys + ['include_trash', 'include_old_versions']).sort, coll_index_params.keys.sort

    include_trash_param = coll_index_params['include_trash']
    assert_equal 'boolean', include_trash_param['type']
    assert_equal false, include_trash_param['required']
    assert_equal 'query', include_trash_param['location']
  end
end
