require_relative 'helper' class GroupsTest < MiniTest::Unit::TestCase 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_save VCR.use_cassette('save_group') do delete_all_groups group = MxHero::API::Group.new(name: 'test') response = @groups.save(group) assert response.is_a? MxHero::API::Response assert response.success? assert response.content.is_a? MxHero::API::Group assert 'test', 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') groups = @groups.all refute groups.any? { |group| group.name == 'test' } end end def test_accounts VCR.use_cassette('accounts') do accounts = @groups.accounts('development') 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 end