Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Glimpse/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class Config
* process fills it in before tagging, and an empty value keeps the
* fallback off.
*/
private const PUBLIC_TOKEN = '';
private const PUBLIC_TOKEN = '4|wbQrxtzJP89PXWn5Mku5fRUjBGLTnbB9javJhN9m5d9cb13f';

private readonly string $publicToken;

Expand Down
16 changes: 14 additions & 2 deletions tests/Feature/Glimpse/PublicTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,22 @@
->and($config->usingPublicToken())->toBeFalse();
});

test('without a baked public token the fallback stays off', function () {
$config = new Config;
test('an empty public token keeps the fallback off', function () {
$config = new Config(publicTokenOverride: '');

expect($config->token())->toBeNull()
->and($config->publicToken())->toBeNull()
->and($config->usingPublicToken())->toBeFalse();
});

test('the baked release token is a plausible Sanctum token when present', function () {
$baked = (new Config)->publicToken();

// Empty in the repository is allowed (the release guard enforces
// baking); when present it must look like id|secret.
if ($baked !== null) {
expect($baked)->toMatch('/^\d+\|\w{40,}$/');
}

expect(true)->toBeTrue();
});
6 changes: 6 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Support\Facades\File;
use MathiasGrimm\GlimpseCli\Glimpse\Config;
use MathiasGrimm\GlimpseCli\Support\BaselineFile;
use MathiasGrimm\GlimpseCli\Support\Sleeper;
use Tests\Fixtures\Images;
Expand All @@ -14,6 +15,11 @@
putenv('XDG_CONFIG_HOME='.$this->configHome);
putenv('GLIMPSE_TOKEN');
putenv('GLIMPSE_API_URL');

// A real public token is baked into Config for releases. Tests
// must not silently fall back to it: run with the fallback off,
// and let tests that exercise it bind their own override.
$this->app->instance(Config::class, new Config(publicTokenOverride: ''));
})
->afterEach(function () {
if ($this->originalCwd !== '') {
Expand Down
18 changes: 9 additions & 9 deletions tests/Unit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

describe('token', function () {
test('returns null when nothing is configured', function () {
expect((new Config)->token())->toBeNull();
expect((new Config(publicTokenOverride: ''))->token())->toBeNull();
});

test('reads the token from the config file', function () {
$config = new Config;
$config = new Config(publicTokenOverride: '');
$config->setToken('file-token');

expect($config->token())->toBe('file-token');
});

test('GLIMPSE_TOKEN env var beats the config file', function () {
$config = new Config;
$config = new Config(publicTokenOverride: '');
$config->setToken('file-token');

putenv('GLIMPSE_TOKEN=env-token');
Expand All @@ -28,19 +28,19 @@

describe('apiUrl', function () {
test('defaults to the production API url', function () {
expect((new Config)->apiUrl())->toBe('https://glimpseimg.com/api');
expect((new Config(publicTokenOverride: ''))->apiUrl())->toBe('https://glimpseimg.com/api');
});

test('GLIMPSE_API_URL env var overrides the default and trailing slashes are trimmed', function () {
putenv('GLIMPSE_API_URL=https://glimpseimg.test/api/');

expect((new Config)->apiUrl())->toBe('https://glimpseimg.test/api');
expect((new Config(publicTokenOverride: ''))->apiUrl())->toBe('https://glimpseimg.test/api');

putenv('GLIMPSE_API_URL');
});

test('reads api_url from the config file', function () {
$config = new Config;
$config = new Config(publicTokenOverride: '');

mkdir(dirname($config->path()), 0700, true);
file_put_contents($config->path(), json_encode(['api_url' => 'https://staging.glimpseimg.com/api']));
Expand All @@ -51,7 +51,7 @@

describe('setToken', function () {
test('creates the config file under XDG_CONFIG_HOME with restrictive permissions', function () {
$config = new Config;
$config = new Config(publicTokenOverride: '');
$config->setToken('secret');

expect($config->path())->toBe($this->configHome.'/glimpse/config.json')
Expand All @@ -60,7 +60,7 @@
});

test('preserves other config keys when updating the token', function () {
$config = new Config;
$config = new Config(publicTokenOverride: '');

mkdir(dirname($config->path()), 0700, true);
file_put_contents($config->path(), json_encode(['api_url' => 'https://staging.glimpseimg.com/api']));
Expand All @@ -72,7 +72,7 @@
});

test('setting a null token removes it from the file', function () {
$config = new Config;
$config = new Config(publicTokenOverride: '');
$config->setToken('secret');
$config->setToken(null);

Expand Down
Loading