Skip to content

Commit

Permalink
fix: Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Aug 27, 2024
1 parent 6114bd9 commit d47aefb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/Models/BackupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
*/
class BackupTask extends Model
{
use ComposesTelegramNotification;
use AuditableModel;
use ComposesTelegramNotification;
/** @use HasFactory<BackupTaskFactory> */
use HasFactory;
use HasTags;
Expand Down
37 changes: 20 additions & 17 deletions resources/views/components/telegram-form.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<template x-if="!$wire.form.value">
<div class="mt-4" x-data="telegram">
<x-primary-button type="button" centered x-on:click="loadData">{{ __('Get my Telegram ID') }}</x-primary-button>
<x-primary-button type="button" centered x-on:click="loadData">
{{ __('Get my Telegram ID') }}
</x-primary-button>
</div>
</template>

Expand All @@ -17,39 +19,40 @@
if (window.Telegram) {
try {
//try to authenticate Telegram user
window.Telegram.Login.auth({
bot_id: "{{ config('services.telegram.bot_id') }}",
request_access: true
window.Telegram.Login.auth(
{
bot_id: '{{ config('services.telegram.bot_id') }}',
request_access: true,
},
(data) => {
//if authentication failed, warn user and log the event at the backend side
if (!data) {
Toaster.error("{{ __('Authorization failed! Please try again.') }}");
Toaster.error('{{ __('Authorization failed! Please try again.') }}');
$wire.dispatchSelf('jsError', {
message: 'Authorization failed on Telegram side'
message: 'Authorization failed on Telegram side',
});
//if authentication succeeded, update the form value
//if authentication succeeded, update the form value
} else {
$wire.form.value = data.id;
}
}
},
);
//if there are problems with our request, warn user and log the event at the backend side
//if there are problems with our request, warn user and log the event at the backend side
} catch (error) {
Toaster.error("{{ __('Something went wrong! Please try again later.') }}");
Toaster.error('{{ __('Something went wrong! Please try again later.') }}');
$wire.dispatchSelf('jsError', {
message: error.message
message: error.message,
});
}
//if Telegram script was not loaded, warn user and log the event at the backend side
//if Telegram script was not loaded, warn user and log the event at the backend side
} else {
Toaster.error("{{ __('Something went wrong! Please reload the page and try again.') }}");
Toaster.error('{{ __('Something went wrong! Please reload the page and try again.') }}');
$wire.dispatch('jsError', {
message: 'There is no Telegram object when trying to authenticate Telegram user.'
message: 'There is no Telegram object when trying to authenticate Telegram user.',
});
}
}
}
})
},
};
});
</script>
@endscript
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@
});

it('submits successfully with Telegram ID and both notification preferences enabled', function (): void {
Config::set('services.telegram.bot_token', '456');
Config::set('services.telegram.bot_id', '123');

$testData = [
'label' => 'Telegram',
'type' => 'telegram',
Expand All @@ -386,6 +389,7 @@
->set('form.success_notification', $testData['success_notification'])
->set('form.failed_notification', $testData['failed_notification'])
->call('submit')
->assertHasNoErrors(['form.type']) // Check specifically for 'form.type' errors
->assertHasNoErrors()
->assertRedirect(route('notification-streams.index'));

Expand Down
19 changes: 13 additions & 6 deletions tests/Unit/Models/BackupTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Models\Tag;
use App\Models\Traits\ComposesTelegramNotification;
use App\Models\User;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Carbon;

uses(ComposesTelegramNotification::class);
Expand Down Expand Up @@ -1030,28 +1031,34 @@
});

it('sends a Telegram notification successfully', function (): void {
$botToken = 'abc123';
$userToken = 'def456';

Config::set('services.telegram.bot_token', $botToken);

$task = BackupTask::factory()->create();
$log = BackupTaskLog::factory()->create(['backup_task_id' => $task->id]);
$userToken = 'def456';

Http::fake([
$this->getTelegramUrl() => Http::response('', 200),
"https://api.telegram.org/bot{$botToken}/sendMessage" => Http::response('', 200),
]);

$task->sendTelegramNotification($log, $userToken);

Http::assertSent(
fn ($request): bool => $request->url() === $this->getTelegramUrl() &&
Http::assertSent(function (Request $request) use ($botToken, $userToken, $task, $log): bool {
return $request->url() === "https://api.telegram.org/bot{$botToken}/sendMessage" &&
$request['text'] === $this->composeTelegramNotificationText($task, $log) &&
$request['chat_id'] === $userToken &&
$request['parse_mode'] === 'HTML'
);
$request['parse_mode'] === 'HTML';
});
});

it('throws an exception when Telegram notification fails', function (): void {
$task = BackupTask::factory()->create();
$log = BackupTaskLog::factory()->create(['backup_task_id' => $task->id]);

Config::set('services.telegram.bot_token', '456');

Http::fake([
$this->getTelegramUrl() => Http::response('Error', 500),
]);
Expand Down

0 comments on commit d47aefb

Please sign in to comment.