require_relative 'helper' class MxHeroAPITest < MiniTest::Unit::TestCase def setup @api = MxHero::API::Client.new(username: TEST_API_USER, password: TEST_API_PASSWORD, verbose: false, api_url: TEST_API_URL) end def test_update_rule VCR.use_cassette('update_rule') do msg = rule_msg_with_domain('tesla.com') msg[:id] = 97 msg[:properties].first[:propertyValue] = 'another content' assert @api.update_rule(msg) rule = @api.domain_rule(msg[:domain], msg[:id]) assert 97, rule[:id] assert 'another content', rule[:properties].first[:propertyValue] end end def test_domain_rule VCR.use_cassette('domain_rule') do rule = @api.domain_rule('tesla.com', 97) assert 97, rule[:id] assert 'tesla.com', rule[:domain] end end def test_rules_for_domain VCR.use_cassette('rules_for_domain') do response = @api.rules_for_domain('tesla.com') assert response.code == 200 rules = response.msg assert_equal 'tesla.com', rules.first[:domain] end end def test_domains VCR.use_cassette('domains') do domains = @api.domains assert domains.key?(:elements) assert domains.key?(:totalElements) assert domains.key?(:totalPages) assert domains.key?(:actualPage) end end def test_domain_by_id VCR.use_cassette('domain_by_id') do id = "test.com" tesla_domain = @api.domain_by_id(id) assert tesla_domain.key?(:domain) assert tesla_domain.key?(:server) assert tesla_domain.key?(:creationDate) assert tesla_domain.key?(:aliases) assert tesla_domain.key?(:ldap) end end def test_domain_by_id_not_found VCR.use_cassette('domain_by_id_not_found') do id = "test.notfound.com" tesla_domain = @api.domain_by_id(id) assert tesla_domain[:status] == 404 end end def test_domain_by_id_param_could_not_be_nil assert_raises(RuntimeError) { @api.domain_by_id } end def test_accounts_by_domain VCR.use_cassette('accounts_from_domain') do domain = 'mxhero.com' response = @api.accounts_by_domain(domain) must_have_the_keys(response, %w( elements totalElements totalPages actualPage )) response[:elements].each do |account| must_have_the_keys(account, %w( account domain createdDate updatedDate group aliases dataSource ) ) account[:aliases].each { |t_alias| must_have_the_keys(t_alias, %w( name domain dataSource )) } end end end def must_have_the_keys(hash, keys) keys.each do |key| assert hash.key?(key.to_sym), "must have the key #{key}" end end def rule_msg_with_domain(domain) msg = create_rule_msg msg[:domain] = domain msg end def create_rule_msg { #"domain" => "tesla.com", twoWays: false, enabled: true, name: "Email footer", created: 1369682598000, fromDirection: { directionType: "domain", domain: "tesla.com", freeValue: "tesla.com" }, toDirection: { directionType: "domain", domain: "tesla.com", freeValue: "tesla.com" }, properties: [{ propertyKey: "return.message", propertyValue: "footer content" }], component: "org.mxhero.feature.disclaimer" } end def test_create_rule VCR.use_cassette('create_rule_success') do response = @api.create_rule(create_rule_msg) assert response.code == 201 end end def test_try_to_create_rule_that_alredy_exist VCR.use_cassette('create_rule_alredy_exist') do response = @api.create_rule(create_rule_msg) assert response.code == 201 new_response = @api.create_rule(create_rule_msg) assert new_response.code == 500 assert_equal "rules.already.exists.for.component", new_response.msg[:developerMessage] end end def test_create_rule_for_domain VCR.use_cassette('create_rule_for_domain') do response = @api.create_rule_for_domain('tesla.com', create_rule_msg.merge({"domain" => "tesla.com"})) assert response.code == 201 end end end