@Test public void getDomainsShouldContainDetectedDomainsAndIpsOfAddedDomains() throws Exception { domainList.configure(DomainListConfiguration.builder() .autoDetect(true) .autoDetectIp(true)); String added = "added.tld"; String detected = "detected.tld"; String detectedIp1 = "148.25.32.1"; String detectedIp2 = "148.25.32.2"; when(dnsService.getLocalHost()).thenReturn(InetAddress.getByName("127.0.0.1")); when(dnsService.getHostName(any(InetAddress.class))).thenReturn(detected); InetAddress detectedAddress1 = mock(InetAddress.class); InetAddress detectedAddress2 = mock(InetAddress.class); when(detectedAddress1.getHostAddress()).thenReturn(detectedIp1); when(detectedAddress2.getHostAddress()).thenReturn(detectedIp2); when(dnsService.getAllByName(detected)).thenReturn(ImmutableList.of(detectedAddress1)); when(dnsService.getAllByName(added)).thenReturn(ImmutableList.of(detectedAddress2)); domainList.addDomain(Domain.of(added)); assertThat(domainList.getDomains()) .extracting(Domain::name) .contains(detected, detectedIp1, added, detectedIp2); }
public static DomainListConfiguration from(HierarchicalConfiguration config) { ImmutableList<Domain> configuredDomains = StreamUtils.ofNullable(config.getStringArray(CONFIGURE_DOMAIN_NAMES)) .filter(s -> !s.isEmpty()) .map(Domain::of) .collect(Guavate.toImmutableList()); return builder() .autoDetect(Optional.ofNullable(config.getBoolean(CONFIGURE_AUTODETECT, null))) .autoDetectIp(Optional.ofNullable(config.getBoolean(CONFIGURE_AUTODETECT_IP, null))) .defaultDomain(Optional.ofNullable(config.getString(CONFIGURE_DEFAULT_DOMAIN, null)) .map(Domain::of)) .addConfiguredDomains(configuredDomains) .build(); }
@BeforeEach void setUp() throws Exception { memoryRecipientRewriteTable = new MemoryRecipientRewriteTable(); DNSService dnsService = mock(DNSService.class); domainList = new MemoryDomainList(dnsService); domainList.configure(DomainListConfiguration.builder() .autoDetect(false) .autoDetectIp(false)); domainList.addDomain(DOMAIN); usersRepository = MemoryUsersRepository.withVirtualHosting(); usersRepository.setDomainList(domainList); usersRepository.configure(new DefaultConfigurationBuilder()); usersRepository.addUser(BOB, BOB_PASSWORD); usersRepository.addUser(ALICE, ALICE_PASSWORD); usersRepository.addUser(ALICE_WITH_SLASH, ALICE_SLASH_PASSWORD); usersRepository.addUser(CEDRIC, CEDRIC_PASSWORD); createServer(new ForwardRoutes(memoryRecipientRewriteTable, usersRepository, new JsonTransformer())); }
@Before @SuppressWarnings("unchecked") public void setUp() throws Exception { domainList = new MemoryDomainList(DNS_SERVICE); domainList.configure(DomainListConfiguration.builder() .autoDetect(false) .autoDetectIp(false) .build()); usersRepository = MemoryUsersRepository.withVirtualHosting(); usersRepository.setDomainList(domainList); testee = new JamesMailetContext(); MailQueueFactory<MailQueue> mailQueueFactory = mock(MailQueueFactory.class); spoolMailQueue = mock(MailQueue.class); when(mailQueueFactory.createQueue(MailQueueFactory.SPOOL)).thenReturn(spoolMailQueue); testee.retrieveRootMailQueue(mailQueueFactory); testee.setDomainList(domainList); testee.setUsersRepository(usersRepository); mailAddress = new MailAddress(USERMAIL); }
@Before public void setUp() throws Exception { UsersRepository users = MemoryUsersRepository.withoutVirtualHosting(); users.addUser(VALID_USER, PASSWORD); MemoryDomainList memoryDomainList = new MemoryDomainList(mock(DNSService.class)); memoryDomainList.configure(DomainListConfiguration.builder() .defaultDomain(Domain.LOCALHOST) .build()); memoryRecipientRewriteTable = new MemoryRecipientRewriteTable(); memoryRecipientRewriteTable.setDomainList(memoryDomainList); handler = new ValidRcptHandler(users, memoryRecipientRewriteTable, memoryDomainList); validUserEmail = new MailAddress(VALID_USER + "@localhost"); user1mail = new MailAddress(USER1 + "@localhost"); invalidUserEmail = new MailAddress(INVALID_USER + "@localhost"); }
public static Builder builder() { return new Builder(); }
public Builder addConfiguredDomains(Domain... domains) { return this.addConfiguredDomains(Arrays.asList(domains)); }
public void configure(DomainListConfiguration.Builder configurationBuilder) throws ConfigurationException { configure(configurationBuilder.build()); }
@Test public void containsDomainShouldReturnFalseWhenDomainIsNotContained() throws Exception { Domain domain = Domain.of("added.tld"); domainList.configure(DomainListConfiguration.builder() .autoDetect(false) .autoDetectIp(false)); assertThat(domainList.containsDomain(domain)).isFalse(); }
@Test public void getDomainsShouldContainDetectedDomainsAndIps() throws Exception { domainList.configure(DomainListConfiguration.builder() .autoDetect(true) .autoDetectIp(true)); String detected = "detected.tld"; String detectedIp = "148.25.32.1"; when(dnsService.getLocalHost()).thenReturn(InetAddress.getByName("127.0.0.1")); when(dnsService.getHostName(any(InetAddress.class))).thenReturn(detected); InetAddress detectedAddress = mock(InetAddress.class); when(detectedAddress.getHostAddress()).thenReturn(detectedIp); when(dnsService.getAllByName(detected)).thenReturn(ImmutableList.of(detectedAddress)); assertThat(domainList.getDomains()).contains(Domain.of(detected), Domain.of(detectedIp)); }
@Test public void configureShouldNotFailWhenConfiguringDefaultDomain() throws Exception { XMLDomainList testee = new XMLDomainList(setUpDNSServer("localhost")); testee.configure(DomainListConfiguration.builder() .autoDetect(false) .autoDetectIp(false) .defaultDomain(Domain.LOCALHOST) .addConfiguredDomain(DOMAIN_1)); assertThat(testee.getDomainListInternal()) .containsOnly(DOMAIN_1, Domain.LOCALHOST); } }
@Test public void configureShouldNotAttemptToChangeLocalHostDefaultDomainWhenNoAutoDetect() throws Exception { domainList.configure(DomainListConfiguration.builder() .autoDetect(false) .autoDetectIp(false) .defaultDomain(Domain.LOCALHOST)); assertThat(domainList.getDefaultDomain()).isEqualTo(Domain.LOCALHOST); }
@Test public void configuredDomainShouldBeAddedUponConfiguration() throws Exception { Domain domain1 = Domain.of("conf1.tld"); Domain domain2 = Domain.of("conf2.tld"); domainList.configure(DomainListConfiguration.builder() .autoDetect(false) .autoDetectIp(false) .addConfiguredDomains(domain1, domain2)); assertThat(domainList.getDomains()).contains(domain1, domain2); }
@Test public void isLocalUserShouldReturnTrueWhenUsedWithLocalPartAndUserExistOnDefaultDomain() throws Exception { domainList.configure(DomainListConfiguration.builder() .autoDetect(false) .autoDetectIp(false) .defaultDomain(DOMAIN_COM) .build()); usersRepository.addUser(USERMAIL, PASSWORD); assertThat(testee.isLocalUser(USERNAME)).isTrue(); }
@Test public void containsDomainShouldReturnTrueWhenDomainIsContained() throws Exception { Domain domain = Domain.of("added.tld"); domainList.addDomain(domain); domainList.configure(DomainListConfiguration.builder() .autoDetect(false) .autoDetectIp(false)); assertThat(domainList.containsDomain(domain)).isTrue(); }
@Test public void getDomainsShouldNotInteractWithDNSWhenDisabled() throws Exception { domainList.configure(DomainListConfiguration.builder() .autoDetect(false) .autoDetectIp(false)); domainList.getDomains(); verifyZeroInteractions(dnsService); }
@Test public void getDomainsShouldNotDetectDomainsWhenDisabled() throws Exception { Domain domain = Domain.of("domain.tld"); domainList.configure(DomainListConfiguration.builder() .autoDetect(false) .autoDetectIp(false) .defaultDomain(domain)); assertThat(domainList.getDomains()).containsOnly(domain); }
@Test public void testGetDomainsAutoDetectLocalHost() throws Exception { XMLDomainList dom = new XMLDomainList(setUpDNSServer("localhost")); dom.configure(DomainListConfiguration.builder() .autoDetect(false) .autoDetectIp(false) .addConfiguredDomains(Domain.of("domain1.")) .defaultDomain(DEFAULT_DOMAIN)); assertThat(dom.getDomains()).hasSize(2); }
@Test public void removeDomainShouldFailWhenAlreadyConfigured() throws Exception { expectedException.expect(DomainListException.class); XMLDomainList testee = new XMLDomainList(setUpDNSServer("localhost")); testee.configure(DomainListConfiguration.builder() .autoDetect(true) .autoDetectIp(false) .addConfiguredDomain(DOMAIN_1)); testee.removeDomain(Domain.of("newDomain")); }
@Test public void envDomainShouldBeAddedUponConfiguration() throws Exception { String envDomain = "env.tld"; when(envDetector.getEnv(AbstractDomainList.ENV_DOMAIN)).thenReturn(envDomain); domainList.configure(DomainListConfiguration.builder() .autoDetect(true) .autoDetectIp(false)); assertThat(domainList.containsDomain(Domain.of(envDomain))).isTrue(); }