Skip to content

Commit

Permalink
#317: Added Google Analytics Tracking Id to server configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
manusa committed Jul 30, 2019
1 parent 654425b commit cd26fc1
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 1 deletion.
17 changes: 17 additions & 0 deletions server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Isotope Mail Client (Server)
============================

HTTP REST API for IMAP/SMTP communications.

Java Back-end application designed as a "microservice", future developments
will break down this application into separate microservices for communications with
different protocols (IMAP, SMTP, etc.) and data domains.

## Configuration variables

Variable | Description
-------- | -----------
ENCRYPTION_PASSWORD | Password used for encryption and decryption (symmetric) of credentials (Password must be the same for all the instances of the microservice in a single deployment)
TRUSTED_HOSTS | Comma separated list of hosts allowed as IMAP/SMTP server parameters, if empty or not provided, all hosts will be allowed.
EMBEDDED_IMAGE_SIZE_THRESHOLD | Size in bytes defining the threshold value used to decide if images will be downloaded separately or embedded within messages when retrieved from the client
GOOGLE_ANALYTICS_TRACKING_ID | \[Optional\] Google Analytics tracking id to enable google analytics in client application
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public ResponseEntity<Credentials> login(
@SuppressWarnings("ConstantConditions")
private ConfigurationDto toDto(IsotopeApiConfiguration configuration) {
final ConfigurationDto ret = new ConfigurationDto();
ret.setGoogleAnalyticsTrackingId(configuration.getGoogleAnalyticsTrackingId());
ret.add(linkTo(methodOn(ApplicationResource.class)
.login(null))
.withRel(REL_APPLICATION_LOGIN));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,41 @@
*/
package com.marcnuri.isotope.api.application;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.marcnuri.isotope.api.resource.IsotopeResource;

import java.io.Serializable;
import java.util.Objects;

/**
* Created by Marc Nuri <[email protected]> on 2019-04-06.
*/
@JsonInclude(JsonInclude.Include.NON_NULL)
class ConfigurationDto extends IsotopeResource implements Serializable {

private static final long serialVersionUID = -1279556906780840837L;

private String googleAnalyticsTrackingId;

public String getGoogleAnalyticsTrackingId() {
return googleAnalyticsTrackingId;
}

public void setGoogleAnalyticsTrackingId(String googleAnalyticsTrackingId) {
this.googleAnalyticsTrackingId = googleAnalyticsTrackingId;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
ConfigurationDto that = (ConfigurationDto) o;
return Objects.equals(googleAnalyticsTrackingId, that.googleAnalyticsTrackingId);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), googleAnalyticsTrackingId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public class IsotopeApiConfiguration {
private static final String EMBEDDED_IMAGE_SIZE_THRESHOLD = "EMBEDDED_IMAGE_SIZE_THRESHOLD";
private static final long EMBEDDED_IMAGE_SIZE_THRESHOLD_DEFAULT_50KB = 51200L;

private static final String GOOGLE_ANALYTICS_TRACKING_ID = "GOOGLE_ANALYTICS_TRACKING_ID";

private static final int CREDENTIALS_DURATION_MINUTES = 15;
private static final int CREDENTIALS_REFRESH_BEFORE_DURATION_MINUTES = 10;

Expand Down Expand Up @@ -87,6 +89,10 @@ public long getEmbeddedImageSizeThreshold() {
return environment.getProperty(EMBEDDED_IMAGE_SIZE_THRESHOLD, Long.class, EMBEDDED_IMAGE_SIZE_THRESHOLD_DEFAULT_50KB);
}

public String getGoogleAnalyticsTrackingId() {
return environment.getProperty(GOOGLE_ANALYTICS_TRACKING_ID);
}

public TemporalAmount getCredentialsDuration() {
return Duration.ofMinutes(CREDENTIALS_DURATION_MINUTES);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ private void setMessagesFlag(URLName folderId, Flags.Flag flag, boolean flagValu
}

/**
* Returns a list of {@link Attachment}s and replaces embedded images in {@link Message#content} if they are
* Returns a list of {@link Attachment}s and replaces embedded images in {@link Message#getContent()} if they are
* small in order to avoid future calls to the API which may result more expensive.
*
* @param finalMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
Expand Down Expand Up @@ -86,6 +87,7 @@ public void getConfiguration_na_shouldReturnOk() throws Exception {
.accept(MediaTypes.HAL_JSON_VALUE));
// Then
result.andExpect(status().isOk());
result.andExpect(jsonPath("$.googleAnalyticsTrackingId").doesNotExist());
result.andExpect(jsonPath("$._links").exists());
result.andExpect(jsonPath("$._links", aMapWithSize(14)));
result.andExpect(jsonPath("$._links['application.login'].href", endsWith("/v1/application/login")));
Expand All @@ -109,6 +111,19 @@ public void getConfiguration_na_shouldReturnOk() throws Exception {
result.andExpect(jsonPath("$._links.smtp.href", endsWith("/v1/smtp")));
}

@Test
public void getConfiguration_optionalEnvVariablesSet_shouldReturnOk() throws Exception {
// Given
doReturn("UA-1337-33").when(isotopeApiConfiguration).getGoogleAnalyticsTrackingId();
// When
final ResultActions result = mockMvc.perform(get("/v1/application/configuration")
.accept(MediaTypes.HAL_JSON_VALUE));
// Then
result.andExpect(status().isOk());
result.andExpect(jsonPath("$.googleAnalyticsTrackingId", is("UA-1337-33")));
result.andExpect(jsonPath("$._links", aMapWithSize(14)));
}

@Test
public void login_validCredentials_shouldReturnOk() throws Exception {
// Given
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* ConfigurationDtoTest.java
*
* Created on 2019-07-29, 6:58
*
* Copyright 2019 Marc Nuri
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.marcnuri.isotope.api.application;

import org.junit.Test;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

/**
* Created by Marc Nuri <[email protected]> on 2019-07-29.
*/
public class ConfigurationDtoTest {

@Test
public void hashCode_sameProperties_shouldBeTrue() {
// Given
final ConfigurationDto one = new ConfigurationDto();
final ConfigurationDto other = new ConfigurationDto();
for (ConfigurationDto instance : new ConfigurationDto[]{one, other}) {
instance.setGoogleAnalyticsTrackingId("UA-1337-33");
}
// When
final int hashCodeOne = one.hashCode();
final int hashCodeOther = other.hashCode();
// Then
assertThat(hashCodeOne, is(hashCodeOther));
}

@Test
public void equals_sameProperties_shouldBeTrue() {
// Given
final ConfigurationDto one = new ConfigurationDto();
final ConfigurationDto other = new ConfigurationDto();
for (ConfigurationDto instance : new ConfigurationDto[]{one, other}) {
instance.setGoogleAnalyticsTrackingId("UA-1337-33");
}
// When
final boolean result = one.equals(other);
// Then
assertThat(result, is(true));
}

@Test
public void equals_differentProperties_shouldBeFalse() {
// Given
final ConfigurationDto one = new ConfigurationDto();
one.setGoogleAnalyticsTrackingId("UA-1337-33");
final ConfigurationDto other = new ConfigurationDto();
// When
final boolean result = one.equals(other);
// Then
assertThat(result, is(false));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* IsotopeApiConfigurationTest.java
*
* Created on 2019-07-28, 20:34
*
* Copyright 2019 Marc Nuri
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.marcnuri.isotope.api.configuration;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.core.env.Environment;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;

/**
* Created by Marc Nuri <[email protected]> on 2019-07-28.
*/
public class IsotopeApiConfigurationTest {

private Environment environment;
private IsotopeApiConfiguration istotopeApiConfiguration;

@Before
public void setUp() {
environment = Mockito.mock(Environment.class);
istotopeApiConfiguration = new IsotopeApiConfiguration(environment);
}

@Test
public void getEncryptionPassword_envVariableSet_shouldReturnEnvValue() {
// Given
doReturn("1234").when(environment)
.getProperty(eq("ENCRYPTION_PASSWORD"), anyString());
// When
final String result = istotopeApiConfiguration.getEncryptionPassword();
// Then
assertThat(result, is("1234"));
}

@Test
public void getGoogleAnalyticsTrackingId_envVariableSet_shouldReturnEnvValue() {
// Given
doReturn("UA-1337-33").when(environment).getProperty(eq("GOOGLE_ANALYTICS_TRACKING_ID"));
// When
final String result = istotopeApiConfiguration.getGoogleAnalyticsTrackingId();
// Then
assertThat(result, is("UA-1337-33"));
}

}

0 comments on commit cd26fc1

Please sign in to comment.