blob: 26ba4d82d11911654c32458f6cdd8d12443a047d [file]
// Copyright 2023 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {assert} from '@open-wc/testing';
import './test-setup';
import {createArtifactLink} from '../resultdb-utils';
import {LinkIcon} from '@gerritcodereview/typescript-api/checks';
suite('resultdb-utils tests', () => {
let sandbox: sinon.SinonSandbox;
setup(() => {
sandbox = sinon.createSandbox();
});
teardown(() => {
sandbox.restore();
});
test('createLinkArtifact returns the fetch URL for non-link-artifacts', async () => {
const artifact = {
artifactId: 'foo',
name: 'foo-name',
fetchUrl: 'https://test.artifacts.storage.com/foo/',
contentType: 'text/plain',
sizeBytes: 10,
};
assert.deepEqual(await createArtifactLink(artifact, false, null), {
url: 'https://test.artifacts.storage.com/foo/',
tooltip: 'foo',
primary: false,
icon: LinkIcon.FILE_PRESENT,
});
});
test('createLinkArtifact returns the link artifact direct URL', async () => {
const fetchStub = sandbox.stub(window, 'fetch');
fetchStub
.withArgs('https://test.artifacts.storage.com/testhaus_logs/?n=50000')
.returns(
Promise.resolve({
ok: true,
text: async () => 'https://cros-test-analytics.appspot.com/test/logs',
} as Response)
);
const artifact = {
artifactId: 'testhaus_logs',
name: 'testhaus_logs',
fetchUrl: 'https://test.artifacts.storage.com/testhaus_logs/',
contentType: 'text/x-uri',
sizeBytes: 10,
};
assert.deepEqual(await createArtifactLink(artifact, false, null), {
url: 'https://cros-test-analytics.appspot.com/test/logs',
tooltip: 'testhaus_logs',
primary: false,
icon: LinkIcon.EXTERNAL,
});
});
test('createLinkArtifact validates URL protocol', async () => {
const fetchStub = sandbox.stub(window, 'fetch');
fetchStub
.withArgs('https://test.artifacts.storage.com/testhaus_logs/?n=50000')
.returns(
Promise.resolve({
ok: true,
text: async () => 'file://cros-test-analytics.appspot.com/test/logs',
} as Response)
);
const artifact = {
artifactId: 'testhaus_logs',
name: 'testhaus_logs_file',
fetchUrl: 'https://test.artifacts.storage.com/testhaus_logs/',
contentType: 'text/x-uri',
sizeBytes: 10,
};
assert.deepEqual(await createArtifactLink(artifact, false, null), {
url: 'https://test.artifacts.storage.com/testhaus_logs/',
tooltip: 'testhaus_logs',
primary: false,
icon: LinkIcon.FILE_PRESENT,
});
});
test('createLinkArtifact validates URL host', async () => {
const fetchStub = sandbox.stub(window, 'fetch');
fetchStub
.withArgs('https://test.artifacts.storage.com/testhaus_logs/?n=50000')
.returns(
Promise.resolve({
ok: true,
text: async () =>
'https://cros-test-analytics.appspot.com.evil/test/logs',
} as Response)
);
fetchStub
.withArgs('https://test.artifacts.storage.com/testhaus_logs/?n=50000')
.returns(
Promise.resolve({
ok: true,
text: async () =>
'https://cros-test-analytics.appspot.com:8080/test/logs',
} as Response)
);
const bad_domain_artifact = {
artifactId: 'testhaus_logs',
name: 'testhaus_logs_evil_host',
fetchUrl: 'https://test.artifacts.storage.com/testhaus_logs/',
contentType: 'text/x-uri',
sizeBytes: 10,
};
const port_artifact = {
artifactId: 'testhaus_logs',
name: 'testhaus_logs_with_port',
fetchUrl: 'https://test.artifacts.storage.com/testhaus_logs/',
contentType: 'text/x-uri',
sizeBytes: 10,
};
assert.deepEqual(
await createArtifactLink(bad_domain_artifact, false, null),
{
url: 'https://test.artifacts.storage.com/testhaus_logs/',
tooltip: 'testhaus_logs',
primary: false,
icon: LinkIcon.FILE_PRESENT,
}
);
assert.deepEqual(await createArtifactLink(port_artifact, false, null), {
url: 'https://test.artifacts.storage.com/testhaus_logs/',
tooltip: 'testhaus_logs',
primary: false,
icon: LinkIcon.FILE_PRESENT,
});
});
test('createLinkArtifact returns the fetch URL for very large link artifacts', async () => {
const artifact = {
artifactId: 'testhaus_logs',
name: 'testhaus_logs_extra_large',
fetchUrl: 'https://test.artifacts.storage.com/testhaus_logs_extra_large/',
contentType: 'text/x-uri',
sizeBytes: 50001,
};
assert.deepEqual(await createArtifactLink(artifact, false, null), {
url: 'https://test.artifacts.storage.com/testhaus_logs_extra_large/',
tooltip: 'testhaus_logs',
primary: false,
icon: LinkIcon.FILE_PRESENT,
});
});
test('createLinkArtifact returns the fetch URL on promise error', async () => {
const fetchStub = sandbox.stub(window, 'fetch');
fetchStub
.withArgs('https://test.artifacts.storage.com/stainless_logs/?n=50000')
.returns(Promise.reject(new Error('bad fetch')));
const artifact = {
artifactId: 'stainless_logs',
name: 'stainless_logs',
fetchUrl: 'https://test.artifacts.storage.com/stainless_logs/',
contentType: 'text/x-uri',
sizeBytes: 10,
};
assert.deepEqual(await createArtifactLink(artifact, false, null), {
url: 'https://test.artifacts.storage.com/stainless_logs/',
tooltip: 'stainless_logs',
primary: false,
icon: LinkIcon.FILE_PRESENT,
});
});
test('createLinkArtifact returns the fetch URL on server error', async () => {
const fetchStub = sandbox.stub(window, 'fetch');
fetchStub
.withArgs(
'https://test.artifacts.storage.com/stainless_logs_server_error/?n=50000'
)
.returns(
Promise.resolve({
ok: false,
status: 500,
text: async () => 'server error',
} as Response)
);
const artifact = {
artifactId: 'stainless_logs',
name: 'stainless_logs_server_error',
fetchUrl:
'https://test.artifacts.storage.com/stainless_logs_server_error/',
contentType: 'text/x-uri',
sizeBytes: 10,
};
assert.deepEqual(await createArtifactLink(artifact, false, null), {
url: 'https://test.artifacts.storage.com/stainless_logs_server_error/',
tooltip: 'stainless_logs',
primary: false,
icon: LinkIcon.FILE_PRESENT,
});
});
test('createLinkArtifact returns the fetch URL for invalid link artifact links', async () => {
const fetchStub = sandbox.stub(window, 'fetch');
fetchStub
.withArgs('https://test.artifacts.storage.com/testhaus_log_text/?n=50000')
.returns(
Promise.resolve({
ok: true,
text: async () =>
'Actual text intended for log files, not a http link',
} as Response)
);
const artifact = {
artifactId: 'testhaus_logs',
name: 'testhaus_log_text',
fetchUrl: 'https://test.artifacts.storage.com/testhaus_log_text/',
contentType: 'text/x-uri',
sizeBytes: 10,
};
assert.deepEqual(await createArtifactLink(artifact, false, null), {
url: 'https://test.artifacts.storage.com/testhaus_log_text/',
tooltip: 'testhaus_logs',
primary: false,
icon: LinkIcon.FILE_PRESENT,
});
});
test('createLinkArtifact caches successful fetches', async () => {
const fetchStub = sandbox.stub(window, 'fetch');
fetchStub.onCall(0).returns(Promise.reject(new Error('bad fetch')));
fetchStub.onCall(1).returns(
Promise.resolve({
ok: true,
text: async () => 'https://cros-test-analytics.appspot.com/test/logs',
} as Response)
);
fetchStub.onCall(2).returns(Promise.reject(new Error('bad fetch')));
const artifact = {
artifactId: 'testhaus_logs',
name: 'testhaus_logs_cache_test',
fetchUrl: 'https://test.artifacts.storage.com/testhaus_logs/',
contentType: 'text/x-uri',
sizeBytes: 10,
};
assert.deepEqual(await createArtifactLink(artifact, false, null), {
url: 'https://test.artifacts.storage.com/testhaus_logs/',
tooltip: 'testhaus_logs',
primary: false,
icon: LinkIcon.FILE_PRESENT,
});
assert.deepEqual(await createArtifactLink(artifact, false, null), {
url: 'https://cros-test-analytics.appspot.com/test/logs',
tooltip: 'testhaus_logs',
primary: false,
icon: LinkIcon.EXTERNAL,
});
assert.deepEqual(await createArtifactLink(artifact, false, null), {
url: 'https://cros-test-analytics.appspot.com/test/logs',
tooltip: 'testhaus_logs',
primary: false,
icon: LinkIcon.EXTERNAL,
});
});
});