Skip to content

Commit

Permalink
Improve chat validation dueto Chat endpoint update
Browse files Browse the repository at this point in the history
  • Loading branch information
hieudion committed Oct 23, 2024
1 parent f37e5ce commit c8ce68c
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,7 @@ private boolean isResponseStatusToRetry(final int status, final String message,
// In case we can't parse the message, error should not be show here
}

if (target.equals(Target.CHAT) && !Pattern.compile(ZendeskConstants.Regex.CHAT_LOGIN_URL).matcher(loginURL).matches()) {
throw new ConfigException("Invalid login url. Check that you are using https://www.zopim.com to import chat data.");
}

if (!target.equals(Target.CHAT) && !Pattern.compile(ZendeskConstants.Regex.LOGIN_URL).matcher(loginURL).matches()) {
if (!Pattern.compile(ZendeskConstants.Regex.LOGIN_URL).matcher(loginURL).matches()) {
throw new ConfigException("Invalid login url. Check that you are using the correct Zendesk url (https://example.zendesk.com/) to import data.");
}

Expand Down Expand Up @@ -214,12 +210,7 @@ else if (status != ZendeskConstants.HttpStatus.TOO_MANY_REQUEST) {
// Won't retry for 4xx range errors except above. Almost they should be ConfigError e.g. 403 Forbidden
if (status / 100 == 4) {
if (status == HttpStatus.SC_UNAUTHORIZED) {
if (target.equals(Target.CHAT)) {
throw new ConfigException("Invalid credentials. Check that you are using your Zopim credentials to import Chat data.");
}
else {
throw new ConfigException("Invalid credentials. Check that you are using your Zendesk credentials to import non-Chat data.");
}
throw new ConfigException("Invalid credentials. Check that you are using your Zendesk credentials.");
}

if (status == HttpStatus.SC_FORBIDDEN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public static class Regex
{
public static final String ID = "_id$";
public static final String LOGIN_URL = "^https?://+[a-z0-9_\\\\-]+(.zendesk.com/?)$";
public static final String CHAT_LOGIN_URL = "^https://www.zopim.com/?$";
}

public static class HttpStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,37 +150,14 @@ public void doGetRetry404()
verify(zendeskRestClient, times(expectedRetryTime)).createHttpClient();
}

@Test
public void doThrowWrongHostExceptionForChatWhen404()
{
setup("doGet404");
ConfigSource configSource = ZendeskTestHelper.getConfigSource("incremental.yml");
configSource.set("auth_method", "oauth");
configSource.set("access_token", "token");
configSource.set("target", "chat");
configSource.set("login_url", "https://abc.zendesk.com");
PluginTask pluginTask = CONFIG_MAPPER.map(configSource, PluginTask.class);

String expectedMessage = "Invalid login url. Check that you are using https://www.zopim.com to import chat data.";
int expectedRetryTime = 1;
try {
zendeskRestClient.doGet("any", pluginTask, false);
fail("Should not reach here");
}
catch (final Exception e) {
assertEquals(expectedMessage, e.getMessage());
}
verify(zendeskRestClient, times(expectedRetryTime)).createHttpClient();
}

@Test
public void doThrowWrongHostExceptionForNonChatWhen404()
{
setup("doGet404");
ConfigSource configSource = ZendeskTestHelper.getConfigSource("incremental.yml");
configSource.set("auth_method", "oauth");
configSource.set("access_token", "token");
configSource.set("login_url", "https://www.zopim.com/");
configSource.set("login_url", "https://www.chat.zendesk.com/");
PluginTask pluginTask = CONFIG_MAPPER.map(configSource, PluginTask.class);

String expectedMessage = "Invalid login url. Check that you are using the correct Zendesk url (https://example.zendesk.com/) to import data.";
Expand All @@ -195,40 +172,17 @@ public void doThrowWrongHostExceptionForNonChatWhen404()
verify(zendeskRestClient, times(expectedRetryTime)).createHttpClient();
}

@Test
public void doThrowCredentialWrongExceptionForChatWhen401()
{
setup("credentialFail401");
ConfigSource configSource = ZendeskTestHelper.getConfigSource("incremental.yml");
configSource.set("auth_method", "oauth");
configSource.set("access_token", "token");
configSource.set("target", "chat");
configSource.set("login_url", "https://abc.zendesk.com");
PluginTask pluginTask = CONFIG_MAPPER.map(configSource, PluginTask.class);

String expectedMessage = "Invalid credentials. Check that you are using your Zopim credentials to import Chat data.";
int expectedRetryTime = 1;
try {
zendeskRestClient.doGet("any", pluginTask, false);
fail("Should not reach here");
}
catch (final Exception e) {
assertEquals(expectedMessage, e.getMessage());
}
verify(zendeskRestClient, times(expectedRetryTime)).createHttpClient();
}

@Test
public void doThrowCredentialWrongExceptionForNonChatWhen401()
{
setup("credentialFail401");
ConfigSource configSource = ZendeskTestHelper.getConfigSource("incremental.yml");
configSource.set("auth_method", "oauth");
configSource.set("access_token", "token");
configSource.set("login_url", "https://www.zopim.com/");
configSource.set("login_url", "https://www.chat.zendesk.com/");
PluginTask pluginTask = CONFIG_MAPPER.map(configSource, PluginTask.class);

String expectedMessage = "Invalid credentials. Check that you are using your Zendesk credentials to import non-Chat data.";
String expectedMessage = "Invalid credentials. Check that you are using your Zendesk credentials.";
int expectedRetryTime = 1;
try {
zendeskRestClient.doGet("any", pluginTask, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public void testFetchData()
JsonNode dataSearchJson = ZendeskTestHelper.getJsonFromFile("data/chat_search.json");
JsonNode dataJson = ZendeskTestHelper.getJsonFromFile("data/chat.json");

when(zendeskRestClient.doGet(eq("https://www.zopim.com/api/v2/chats/search?q=timestamp%3A%5B2018-09-15T05%3A00%3A00Z+TO+2019-09-29T05%3A00%3A00Z%5D&page=1"), any(), anyBoolean())).thenReturn(dataSearchJson.toString());
when(zendeskRestClient.doGet(eq("https://www.chat.zendesk.com/api/v2/chats/search?q=timestamp%3A%5B2018-09-15T05%3A00%3A00Z+TO+2019-09-29T05%3A00%3A00Z%5D&page=1"), any(), anyBoolean())).thenReturn(dataSearchJson.toString());

when(zendeskRestClient.doGet(eq("https://www.zopim.com/api/v2/chats?ids=id_1%2Cid_2"), any(), anyBoolean())).thenReturn(dataJson.toString());
when(zendeskRestClient.doGet(eq("https://www.chat.zendesk.com/api/v2/chats?ids=id_1%2Cid_2"), any(), anyBoolean())).thenReturn(dataJson.toString());

zendeskChatService.fetchData("2018-09-15T05:00:00Z", "2019-09-29T05:00:00Z", 1, recordImporter);

Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/config/chat.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type: zendesk
login_url: https://www.zopim.com
login_url: https://www.chat.zendesk.com
auth_method: basic
username: username
password: password
Expand Down
4 changes: 2 additions & 2 deletions src/test/resources/data/chat_search.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"id": "id_1",
"timestamp": "2019-10-12T21:43:23Z",
"preview": "abc",
"url": "https://www.zopim.com/api/v2/chats/id_1",
"url": "https://www.chat.zendesk.com/api/v2/chats/id_1",
"type": "offline_msg"
},
{
"id": "id_2",
"timestamp": "2019-10-12T20:02:54Z",
"preview": "abc",
"url": "https://www.zopim.com/api/v2/chats/id_2",
"url": "https://www.chat.zendesk.com/api/v2/chats/id_2",
"type": "offline_msg"
}
],
Expand Down

0 comments on commit c8ce68c

Please sign in to comment.