require_relative 'helper'

class GroupsTest < Minitest::Test
  def setup
    @groups = MxHero::API::Groups.new(domain, 
                                   username: TEST_API_USER, 
                                   password: TEST_API_PASSWORD, 
                                   verbose: false, api_url: TEST_API_URL)
  end

  def domain
    TEST_API_DOMAIN
  end

  def test_all
    VCR.use_cassette('all_groups') do
      groups = @groups.all
      [:total_elements, :total_pages, :actual_page].each do |property|
        assert groups.respond_to?(property), "Must be contain #{property}"
      end
      groups.first.is_a? MxHero::API::Group
      groups.any? { |group| group.name == 'development' }
    end
  end

  def test_all_to_json
    VCR.use_cassette('all_groups') do
      groups = @groups.all
      json = groups.to_json
      assert json.is_a? String
    end
  end

  def test_save
    VCR.use_cassette('save_group') do
      delete_all_groups
      group = MxHero::API::Group.new(name: 'test group') 
      response = @groups.save(group)
      assert response.is_a? MxHero::API::Response
      #puts response
      assert response.success?
      assert response.content.is_a? MxHero::API::Group
      assert 'test group', response.content.name

      response = @groups.save(group)
      refute response.success?
    end
  end

  def delete_all_groups
    @groups.all.each { |group| @groups.delete(group.name) }
  end

  def test_delete
    VCR.use_cassette('delete_group') do
      assert @groups.delete('test group')
      groups = @groups.all
      refute groups.any? { |group| group.name == 'test group' }
    end
  end
  
  def test_accounts
    VCR.use_cassette('accounts') do 
      accounts = @groups.accounts('development')
      assert accounts.first.is_a? MxHero::API::Account

      #accounts = @groups.account('with spaces')
      #assert accounts.first.is_a? MxHero::API::Account
    end
  end

  def test_add_and_remove_account
    VCR.use_cassette('add_and_remove_account') do
      account_name = 'john.doe'
      group = 'development'
      if @groups.accounts(group).any? { |account| account.account == account_name }
        @groups.remove_account(group, account_name)
      end
      assert @groups.add_account(group, account_name).success?
      assert @groups.accounts(group).any? { |account| account.account == account_name }
      assert @groups.remove_account(group, account_name).success?
      refute @groups.accounts(group).any? { |account| account.account == account_name }
    end
  end

  def test_try_to_add_an_inexistent_account_to_group
    VCR.use_cassette('add_an_inexistent_account_to_group') do
      refute @groups.add_account('development', 'account.is.not.exist').success?
    end
  end

  def test_try_to_remove_account_from_group_twice
    VCR.use_cassette('remove_account_from_group_twice') do
      account = @groups.accounts('development').first
      unless account
        account = 'john.doe' 
        @groups.add_account('development', 'john.doe')
      end
      assert @groups.remove_account('development', account.account).success?
      remove_again = @groups.remove_account('development', account.account)
      refute remove_again.success?
      assert_equal 'domain.group.account.not.in.group', remove_again.content[:developerMessage]
      @groups.add_account('development', account.account)
    end
  end

  def test_try_to_remove_inexistent_account 
    VCR.use_cassette('remove_inexistente_account_from_group') do
      response = @groups.remove_account('development', 'non.existent.account')
      refute response.success?
      assert_equal 'domain.account.not.found', response.content[:developerMessage] 
    end
  end
  

  def test_pagination
    assert @groups.send(:pagination_query).empty?
    assert_equal '?offset=10', @groups.send(:pagination_query, page: 10)
    assert_equal '?limit=10', @groups.send(:pagination_query, per_page: 10)
    assert_equal '?limit=10&offset=2', @groups.send(:pagination_query, per_page: 10, page: 2)
  end

	def test_search_accounts
		VCR.use_cassette('search_accounts_starts_with_a') do
			response = @groups.search_accounts('a')
			assert response.success?
			assert_equal 3, response.msg[:totalElements]
			assert_equal [ 'abuse', 'antonio', 'archive' ], response.msg[:elements].map { |e| e[:account] }
		end

		VCR.use_cassette('search_accounts_starts_with_ab') do
			response = @groups.search_accounts('ab')
			assert response.success?
			assert_equal 1, response.msg[:totalElements]
			assert_equal [ 'abuse' ], response.msg[:elements].map { |e| e[:account] }
		end
	end

end