Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
RhysLees committed Jul 3, 2023
1 parent d5c0513 commit 5b7a66b
Show file tree
Hide file tree
Showing 16 changed files with 485 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ $uploadResponse = $connector->send(

$token = $uploadResponse->dto()->token;

// And to pass to your ticket request

$ticketResponse = $connector->send(
new CreateSingleTicketRequest(
SingleTicketDTO::fromArray([
Expand Down
14 changes: 9 additions & 5 deletions src/ZendeskConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ protected function defaultHeaders(): array
}

protected function defaultAuth(): ?Authenticator
{
$authenticationString = $this->setAuth();
return new TokenAuthenticator(base64_encode($authenticationString), 'Basic');
}

public function setAuth(): string
{
if (! config('zendesk.auth.method')) {
throw new \Exception('No authentication method provided.', 500);
Expand All @@ -47,12 +53,10 @@ protected function defaultAuth(): ?Authenticator
throw new \Exception('No API token provided for token authentication.', 500);
}

$authenticationString = match (config('zendesk.auth.method')) {
'basic' => $authenticationString = config('zendesk.auth.email_address').':'.config('zendesk.auth.password'),
'token' => $authenticationString = config('zendesk.auth.email_address').'/token:'.config('zendesk.auth.api_token'),
return match (config('zendesk.auth.method')) {
'basic' => config('zendesk.auth.email_address').':'.config('zendesk.auth.password'),
'token' => config('zendesk.auth.email_address').'/token:'.config('zendesk.auth.api_token'),
default => throw new \Exception('Invalid authentication method provided.', 500),
};

return new TokenAuthenticator(base64_encode($authenticationString), 'Basic');
}
}
169 changes: 169 additions & 0 deletions tests/Connectors/ZendeskConnectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

use CodebarAg\Zendesk\Enums\TicketPriority;
use CodebarAg\Zendesk\Requests\AllTicketsRequest;
use CodebarAg\Zendesk\ZendeskConnector;
use Saloon\Http\Faking\MockResponse;
use Saloon\Laravel\Http\Faking\MockClient;

it('will throw an exception if a subdomain is not set', closure: function () {
$connector = new ZendeskConnector;
$connector->resolveBaseUrl();

})->throws('No subdomain provided.', 500);

it('will not throw an exception if a subdomain is set', closure: function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag'
]);

$connector = new ZendeskConnector;
$connector->resolveBaseUrl();

})->expectNotToPerformAssertions();

it('will return the base path', closure: function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag'
]);

$connector = new ZendeskConnector;
$path = $connector->resolveBaseUrl();

expect($path)->toBe('https://codebarsolutionsag.zendesk.com/api/v2');

});


it('will throw an exception if an auth method is not set', closure: function () {
config([
'zendesk.auth.method' => null,
]);

$connector = new ZendeskConnector;
$connector->setAuth();

})->throws('No authentication method provided.', 500);

it('will throw an exception if an auth method invalid', closure: function () {
config([
'zendesk.auth.method' => 'not-a-valid-method',
]);

$connector = new ZendeskConnector;
$connector->setAuth();

})->throws('Invalid authentication method provided.', 500);

it('will not throw an exception if an auth method valid', closure: function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
'zendesk.auth.email_address' => '[email protected]',
'zendesk.auth.api_token' => 'test-token',
'zendesk.auth.password' => 'test-password',
]);


config([
'zendesk.auth.method' => 'token',
]);

$connector = new ZendeskConnector;
$connector->setAuth();

config([
'zendesk.auth.method' => 'basic',
]);

$connector = new ZendeskConnector;
$connector->setAuth();

})->expectNotToPerformAssertions();

it('will throw an exception if a token is not provided when using the token method', closure: function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
'zendesk.auth.method' => 'token',
'zendesk.auth.email_address' => '[email protected]',
'zendesk.auth.api_token' => null,
'zendesk.auth.password' => null,
]);

$connector = new ZendeskConnector;
$connector->setAuth();

})->throws('No API token provided for token authentication.', 500);

it('will not throw an exception if a token is provided when using the token method', closure: function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
'zendesk.auth.method' => 'token',
'zendesk.auth.email_address' => '[email protected]',
'zendesk.auth.api_token' => 'test-token',
'zendesk.auth.password' => null,
]);

$connector = new ZendeskConnector;
$connector->setAuth();

})->expectNotToPerformAssertions();

it('will throw an exception if a password is not provided when using the basic method', closure: function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
'zendesk.auth.method' => 'basic',
'zendesk.auth.email_address' => '[email protected]',
'zendesk.auth.api_token' => null,
'zendesk.auth.password' => null,
]);

$connector = new ZendeskConnector;
$connector->setAuth();

})->throws('No password provided for basic authentication.', 500);

it('will not throw an exception if a password is provided when using the password method', closure: function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
'zendesk.auth.method' => 'basic',
'zendesk.auth.email_address' => '[email protected]',
'zendesk.auth.api_token' => null,
'zendesk.auth.password' => 'test-password',
]);

$connector = new ZendeskConnector;
$connector->setAuth();

})->expectNotToPerformAssertions();

it('will compile the correct authentication string for token method', function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
'zendesk.auth.method' => 'token',
'zendesk.auth.email_address' => '[email protected]',
'zendesk.auth.api_token' => 'test-token',
'zendesk.auth.password' => null,
]);

$connector = new ZendeskConnector;

$token = $connector->setAuth();

expect($token)->toBe('[email protected]/token:test-token');
});

it('will compile the correct authentication string for basic method', function () {
config([
'zendesk.subdomain' => 'codebarsolutionsag',
'zendesk.auth.method' => 'basic',
'zendesk.auth.email_address' => '[email protected]',
'zendesk.auth.api_token' => null,
'zendesk.auth.password' => 'test-password',
]);

$connector = new ZendeskConnector;

$token = $connector->setAuth();

expect($token)->toBe('[email protected]:test-password');
});
Binary file added tests/Fixtures/Files/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions tests/Fixtures/Saloon/all-tickets-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"statusCode": 200,
"headers": {
"Date": "Mon, 03 Jul 2023 11:31:03 GMT",
"Content-Type": "application\/json; charset=utf-8",
"Transfer-Encoding": "chunked",
"Connection": "keep-alive",
"x-zendesk-api-version": "v2",
"x-zendesk-application-version": "v17438",
"x-frame-options": "SAMEORIGIN",
"zendesk-rate-limit-tickets-index": "total=100; remaining=99; resets=57",
"x-rate-limit": "400",
"rate-limit": "400",
"x-rate-limit-remaining": "399",
"rate-limit-remaining": "399",
"rate-limit-reset": "57",
"strict-transport-security": "max-age=31536000;",
"etag": "W\/\"8cdd07d5c75ffd1b225da69dee7e9985\"",
"cache-control": "max-age=0, private, must-revalidate",
"x-zendesk-origin-server": "classic-app-server-6fc8db697c-cgrcj",
"set-cookie": [
"_zendesk_cookie=BAhJIhl7ImRldmljZV90b2tlbnMiOnt9fQY6BkVU--0bf2100788cb010d0183feca16aaf88ccaf719ca; path=\/; expires=Wed, 03 Jul 2024 04:36:17 GMT; secure; HttpOnly; SameSite=None",
"__cfruid=96c8e6e12fd2ae1a1fb6295fcc3d95a8a1bb0890-1688383863; path=\/; domain=.codebarsolutionsag.zendesk.com; HttpOnly; Secure; SameSite=None"
],
"x-request-id": [
"7e0eccc609fa7692-LHR",
"7e0eccc609fa7692-LHR"
],
"x-runtime": "0.124075",
"X-Zendesk-Zorg": "yes",
"CF-Cache-Status": "DYNAMIC",
"Report-To": "{\"endpoints\":[{\"url\":\"https:\\\/\\\/a.nel.cloudflare.com\\\/report\\\/v3?s=FrrIioe8zsDqyeop5JgV0cpPiCkfW62wcV7EiVd%2F0fYtWuHvzB5RW1ln%2FfyR4Zbuk7fIcfkLKjUQ8qj%2Fpi5htVqKXZJKY%2FMxVXu4YyHBaDW3pp6%2BIDFYAOAwYHwKH2cz1D0car5EemABOXu9BrdQog%3D%3D\"}],\"group\":\"cf-nel\",\"max_age\":604800}",
"NEL": "{\"success_fraction\":0.01,\"report_to\":\"cf-nel\",\"max_age\":604800}",
"Server": "cloudflare",
"CF-RAY": "7e0eccc609fa7692-LHR"
},
"data": "{\"tickets\":[{\"url\":\"https:\/\/codebarsolutionsag.zendesk.com\/api\/v2\/tickets\/79.json\",\"id\":79,\"external_id\":null,\"via\":{\"channel\":\"api\",\"source\":{\"from\":{},\"to\":{},\"rel\":null}},\"created_at\":\"2023-07-01T12:50:02Z\",\"updated_at\":\"2023-07-01T13:05:54Z\",\"type\":null,\"subject\":\"Eine neue Anfrage der B\u00e4rtschi ist eingetroffen: Projekt #1.\",\"raw_subject\":\"Eine neue Anfrage der B\u00e4rtschi ist eingetroffen: Projekt #1.\",\"description\":\"Eine neue Anfrage der B\u00e4rtschi ist eingetroffen.\\n\\nAnfrage\\nIdentifikation 1\\nBemerkung asdasd\\nURL https:\/\/app.pv.test\/nova\/resources\/inquiries\/1\\nErstellt am 01.07.2023 14:21 Uhr\\n**Kunde**\\nKunden B\u00e4rtschi\\nKunden ID 437046\\nBenutzer ID 437046_ralph\\nBenutzername Ralph Senn\\n**Konfiguration**\\nName Projekt #1\\nDachform Sattel-\/Pultdach\\nMontage Aufdach\\nHerkunft Asiatisch\\nWechselrichter Typ String\\n**Adresse**\\nStrasse M\u00fchlematten 12\\nPLZ 4455\\nOrt Zunzgen\\nLand CH\",\"priority\":\"urgent\",\"status\":\"open\",\"recipient\":null,\"requester_id\":17145664265741,\"submitter_id\":17145664265741,\"assignee_id\":17145664265741,\"organization_id\":17145651654157,\"group_id\":17145664762125,\"collaborator_ids\":[],\"follower_ids\":[],\"email_cc_ids\":[],\"forum_topic_id\":null,\"problem_id\":null,\"has_incidents\":false,\"is_public\":true,\"due_at\":null,\"tags\":[],\"custom_fields\":[{\"id\":17146061349901,\"value\":null},{\"id\":17195718961677,\"value\":null},{\"id\":17195752153741,\"value\":null}],\"satisfaction_rating\":null,\"sharing_agreement_ids\":[],\"custom_status_id\":17145678334989,\"fields\":[{\"id\":17146061349901,\"value\":null},{\"id\":17195718961677,\"value\":null},{\"id\":17195752153741,\"value\":null}],\"followup_ids\":[],\"ticket_form_id\":17145664563725,\"brand_id\":17145651602957,\"allow_channelback\":false,\"allow_attachments\":true,\"from_messaging_channel\":false},{\"url\":\"https:\/\/codebarsolutionsag.zendesk.com\/api\/v2\/tickets\/81.json\",\"id\":81,\"external_id\":null,\"via\":{\"channel\":\"api\",\"source\":{\"from\":{},\"to\":{},\"rel\":null}},\"created_at\":\"2023-07-03T11:16:03Z\",\"updated_at\":\"2023-07-03T11:16:05Z\",\"type\":null,\"subject\":\"My printer is on fire!\",\"raw_subject\":\"My printer is on fire!\",\"description\":\"The smoke is very colorful.\",\"priority\":\"urgent\",\"status\":\"open\",\"recipient\":null,\"requester_id\":17145664265741,\"submitter_id\":17145664265741,\"assignee_id\":17145664265741,\"organization_id\":17145651654157,\"group_id\":17145664762125,\"collaborator_ids\":[],\"follower_ids\":[],\"email_cc_ids\":[],\"forum_topic_id\":null,\"problem_id\":null,\"has_incidents\":false,\"is_public\":true,\"due_at\":null,\"tags\":[],\"custom_fields\":[{\"id\":17146061349901,\"value\":null},{\"id\":17195718961677,\"value\":\"Check field works\"},{\"id\":17195752153741,\"value\":\"Check field works number 2\"}],\"satisfaction_rating\":null,\"sharing_agreement_ids\":[],\"custom_status_id\":17145678334989,\"fields\":[{\"id\":17146061349901,\"value\":null},{\"id\":17195718961677,\"value\":\"Check field works\"},{\"id\":17195752153741,\"value\":\"Check field works number 2\"}],\"followup_ids\":[],\"ticket_form_id\":17145664563725,\"brand_id\":17145651602957,\"allow_channelback\":false,\"allow_attachments\":true,\"from_messaging_channel\":false}],\"next_page\":null,\"previous_page\":null,\"count\":2}"
}
1 change: 1 addition & 0 deletions tests/Fixtures/Saloon/count-tickets-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"statusCode":200,"headers":{"Date":"Mon, 03 Jul 2023 13:19:53 GMT","Content-Type":"application\/json; charset=utf-8","Transfer-Encoding":"chunked","Connection":"keep-alive","x-zendesk-api-version":"v2","x-zendesk-application-version":"v17438","x-frame-options":"SAMEORIGIN","x-rate-limit":"400","rate-limit":"400","x-rate-limit-remaining":"399","rate-limit-remaining":"399","rate-limit-reset":"7","strict-transport-security":"max-age=31536000;","etag":"W\/\"dd5b53a4c6c7cd54d9e75cb4d51c3a8a\"","cache-control":"max-age=0, private, must-revalidate","x-zendesk-origin-server":"classic-app-server-6fc8db697c-msbrs","set-cookie":["_zendesk_cookie=BAhJIhl7ImRldmljZV90b2tlbnMiOnt9fQY6BkVU--0bf2100788cb010d0183feca16aaf88ccaf719ca; path=\/; expires=Wed, 03 Jul 2024 04:36:28 GMT; secure; HttpOnly; SameSite=None","__cfruid=0e155e55bd96d1d52b7c0b19c4c735ae1b2e0d93-1688390393; path=\/; domain=.codebarsolutionsag.zendesk.com; HttpOnly; Secure; SameSite=None"],"x-request-id":["7e0f6c36fb797735-LHR","7e0f6c36fb797735-LHR"],"x-runtime":"0.073519","X-Zendesk-Zorg":"yes","CF-Cache-Status":"DYNAMIC","Report-To":"{\"endpoints\":[{\"url\":\"https:\\\/\\\/a.nel.cloudflare.com\\\/report\\\/v3?s=bl0dv422vOkNLREKNLdaQy5qTMuOaeDMK0kHOBFaaHa6bdBul%2FWMn6KIhIC1GeWb8Z5cXJMcbSVQhZgHH4KjUiJYs%2FFPWcZQD7vYwvy7SWODBlcd6FSqwVYFnq8ghN8Vq6a45PZLHAy2EtL2BEFDqg%3D%3D\"}],\"group\":\"cf-nel\",\"max_age\":604800}","NEL":"{\"success_fraction\":0.01,\"report_to\":\"cf-nel\",\"max_age\":604800}","Server":"cloudflare","CF-RAY":"7e0f6c36fb797735-LHR"},"data":"{\"count\":{\"value\":4,\"refreshed_at\":\"2023-07-03T13:19:53+00:00\"}}"}
40 changes: 40 additions & 0 deletions tests/Fixtures/Saloon/create-attachment-request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"statusCode": 201,
"headers": {
"Date": "Mon, 03 Jul 2023 13:33:16 GMT",
"Content-Type": "application\/json; charset=utf-8",
"Content-Length": "2293",
"Connection": "keep-alive",
"x-zendesk-api-version": "v2",
"x-zendesk-application-version": "v17438",
"x-frame-options": "SAMEORIGIN",
"location": "https:\/\/codebarsolutionsag.zendesk.com\/api\/v2\/attachments\/17278923683597.json",
"access-control-allow-origin": "*",
"access-control-expose-headers": "X-Zendesk-API-Warn,X-Zendesk-User-Id,X-Zendesk-User-Session-Expires-At",
"x-rate-limit": "400",
"rate-limit": "400",
"x-rate-limit-remaining": "399",
"rate-limit-remaining": "399",
"rate-limit-reset": "44",
"strict-transport-security": "max-age=31536000;",
"etag": "W\/\"387b535ca927c3e885bc4ebf45675c64\"",
"cache-control": "max-age=0, private, must-revalidate",
"x-zendesk-origin-server": "classic-app-server-6fc8db697c-lbxfk",
"set-cookie": [
"_zendesk_cookie=BAhJIhl7ImRldmljZV90b2tlbnMiOnt9fQY6BkVU--0bf2100788cb010d0183feca16aaf88ccaf719ca; path=\/; expires=Wed, 03 Jul 2024 07:05:39 GMT; secure; HttpOnly; SameSite=None",
"__cfruid=9a71e3de0932ad74b42e7fcf2ed091e67ad18b84-1688391196; path=\/; domain=.codebarsolutionsag.zendesk.com; HttpOnly; Secure; SameSite=None"
],
"x-request-id": [
"7e0f7fcbdbec7545-LHR",
"7e0f7fcbdbec7545-LHR"
],
"x-runtime": "0.374089",
"X-Zendesk-Zorg": "yes",
"CF-Cache-Status": "DYNAMIC",
"Report-To": "{\"endpoints\":[{\"url\":\"https:\\\/\\\/a.nel.cloudflare.com\\\/report\\\/v3?s=TL6Q7BjsnUmgl8FO0vyKlypUJhEHM8ywbUzWFRH46niQLknkM8iDggX2mC7pWJJ8V8iBHBrGs0U8%2FGwAQEyClKMQMHTIDmcs%2Bl%2BlOH339vU0XQ%2FSAlPzIG1WkX6vs5k%2FCZCmES8PcDYJ0I44ZqeZIg%3D%3D\"}],\"group\":\"cf-nel\",\"max_age\":604800}",
"NEL": "{\"success_fraction\":0.01,\"report_to\":\"cf-nel\",\"max_age\":604800}",
"Server": "cloudflare",
"CF-RAY": "7e0f7fcbdbec7545-LHR"
},
"data": "{\"upload\":{\"token\":\"OPvgMbfg5Der4DYn66hTC31in\",\"expires_at\":\"2023-07-06T13:33:15Z\",\"attachments\":[{\"url\":\"https:\/\/codebarsolutionsag.zendesk.com\/api\/v2\/attachments\/17278923683597.json\",\"id\":17278923683597,\"file_name\":\"test.png\",\"content_url\":\"https:\/\/codebarsolutionsag.zendesk.com\/attachments\/token\/52HCaRgRZM38MvaUuUQsYzhUA\/?name=test.png\",\"mapped_content_url\":\"https:\/\/codebarsolutionsag.zendesk.com\/attachments\/token\/52HCaRgRZM38MvaUuUQsYzhUA\/?name=test.png\",\"content_type\":\"image\/png\",\"size\":26271,\"width\":640,\"height\":360,\"inline\":false,\"deleted\":false,\"malware_access_override\":false,\"malware_scan_result\":\"not_scanned\",\"thumbnails\":[{\"url\":\"https:\/\/codebarsolutionsag.zendesk.com\/api\/v2\/attachments\/17278921757709.json\",\"id\":17278921757709,\"file_name\":\"test_thumb.png\",\"content_url\":\"https:\/\/codebarsolutionsag.zendesk.com\/attachments\/token\/gvLU5FyT7CoKakThdKaxHwVNg\/?name=test_thumb.png\",\"mapped_content_url\":\"https:\/\/codebarsolutionsag.zendesk.com\/attachments\/token\/gvLU5FyT7CoKakThdKaxHwVNg\/?name=test_thumb.png\",\"content_type\":\"image\/png\",\"size\":1823,\"width\":80,\"height\":45,\"inline\":false,\"deleted\":false,\"malware_access_override\":false,\"malware_scan_result\":\"not_scanned\"}]}],\"attachment\":{\"url\":\"https:\/\/codebarsolutionsag.zendesk.com\/api\/v2\/attachments\/17278923683597.json\",\"id\":17278923683597,\"file_name\":\"test.png\",\"content_url\":\"https:\/\/codebarsolutionsag.zendesk.com\/attachments\/token\/52HCaRgRZM38MvaUuUQsYzhUA\/?name=test.png\",\"mapped_content_url\":\"https:\/\/codebarsolutionsag.zendesk.com\/attachments\/token\/52HCaRgRZM38MvaUuUQsYzhUA\/?name=test.png\",\"content_type\":\"image\/png\",\"size\":26271,\"width\":640,\"height\":360,\"inline\":false,\"deleted\":false,\"malware_access_override\":false,\"malware_scan_result\":\"not_scanned\",\"thumbnails\":[{\"url\":\"https:\/\/codebarsolutionsag.zendesk.com\/api\/v2\/attachments\/17278921757709.json\",\"id\":17278921757709,\"file_name\":\"test_thumb.png\",\"content_url\":\"https:\/\/codebarsolutionsag.zendesk.com\/attachments\/token\/gvLU5FyT7CoKakThdKaxHwVNg\/?name=test_thumb.png\",\"mapped_content_url\":\"https:\/\/codebarsolutionsag.zendesk.com\/attachments\/token\/gvLU5FyT7CoKakThdKaxHwVNg\/?name=test_thumb.png\",\"content_type\":\"image\/png\",\"size\":1823,\"width\":80,\"height\":45,\"inline\":false,\"deleted\":false,\"malware_access_override\":false,\"malware_scan_result\":\"not_scanned\"}]}}}"
}
Loading

0 comments on commit 5b7a66b

Please sign in to comment.