Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement source file upload #2859

Merged
merged 4 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 2 additions & 0 deletions src/cmd/sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default function sign(
verbose,
channel,
amoMetadata,
versionSource,
webextVersion,
},
{
Expand Down Expand Up @@ -152,6 +153,7 @@ export default function sign(
validationCheckTimeout: timeout,
approvalCheckTimeout:
approvalTimeout !== undefined ? approvalTimeout : timeout,
versionSource,
});
} else {
const {
Expand Down
5 changes: 5 additions & 0 deletions src/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,11 @@ Example: $0 --help run.
'Only used with `use-submission-api`',
type: 'string',
},
'version-source': {
eviljeff marked this conversation as resolved.
Show resolved Hide resolved
describe:
'Path to a zip file containing human readable source code for a version. ' +
eviljeff marked this conversation as resolved.
Show resolved Hide resolved
'Only used with `use-submission-api`',
eviljeff marked this conversation as resolved.
Show resolved Hide resolved
},
},
)
.command('run', 'Run the extension', commands.run, {
Expand Down
49 changes: 45 additions & 4 deletions src/util/submit-addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,27 @@ export default class Client {
return this.fetchJson(url, 'PUT', JSON.stringify(jsonData));
}

async doFormDataPatch(data, addonId, versionId) {
const patchUrl = new URL(
`addon/${addonId}/versions/${versionId}/`,
this.apiUrl,
);
try {
const formData = new FormData();
for (const field in data) {
formData.set(field, data[field]);
}

const response = await this.fetch(patchUrl, 'PATCH', formData);
if (!response.ok) {
throw new Error(`response status was ${response.status}`);
}
} catch (error) {
log.info(`Upload of ${Object.keys(data)} failed: ${error}.`);
throw new Error(`Uploading ${Object.keys(data)} failed`);
}
}

async doAfterSubmit(addonId, newVersionId, editUrl) {
if (this.approvalCheckTimeout > 0) {
const fileUrl = new URL(
Expand Down Expand Up @@ -237,7 +258,7 @@ export default class Client {
}

async fetch(url, method = 'GET', body) {
log.info(`Fetching URL: ${url.href}`);
log.info(`${method}ing URL: ${url.href}`);
Rob--W marked this conversation as resolved.
Show resolved Hide resolved
let headers = {
Authorization: await this.apiAuth.getAuthHeader(),
Accept: 'application/json',
Expand Down Expand Up @@ -350,13 +371,19 @@ export default class Client {
uploadUuid,
savedIdPath,
metaDataJson,
versionPatchData,
saveIdToFileFunc = saveIdToFile,
) {
const {
guid: addonId,
version: { id: newVersionId, edit_url: editUrl },
} = await this.doNewAddonSubmit(uploadUuid, metaDataJson);

if (versionPatchData) {
log.info('Submitting source zip');
await this.doFormDataPatch(versionPatchData, addonId, newVersionId);
}

await saveIdToFileFunc(savedIdPath, addonId);
log.info(`Generated extension ID: ${addonId}.`);
log.info('You must add the following to your manifest:');
Expand All @@ -365,11 +392,15 @@ export default class Client {
return this.doAfterSubmit(addonId, newVersionId, editUrl);
}

async putVersion(uploadUuid, addonId, metaDataJson) {
async putVersion(uploadUuid, addonId, metaDataJson, versionPatchData) {
const {
version: { id: newVersionId, edit_url: editUrl },
} = await this.doNewAddonOrVersionSubmit(addonId, uploadUuid, metaDataJson);

if (versionPatchData) {
log.info('Submitting source zip');
await this.doFormDataPatch(versionPatchData, addonId, newVersionId);
}
return this.doAfterSubmit(addonId, newVersionId, editUrl);
}
}
Expand All @@ -388,6 +419,7 @@ export async function signAddon({
savedIdPath,
savedUploadUuidPath,
metaDataJson = {},
versionSource,
userAgentString,
SubmitClient = Client,
ApiAuthClass = JwtApiAuth,
Expand Down Expand Up @@ -423,14 +455,23 @@ export async function signAddon({
channel,
savedUploadUuidPath,
);
// if we have a source file we need to upload we patch after the create
const versionPatchData = versionSource
eviljeff marked this conversation as resolved.
Show resolved Hide resolved
? { source: client.fileFromSync(versionSource) }
: undefined;

// We specifically need to know if `id` has not been passed as a parameter because
// it's the indication that a new add-on should be created, rather than a new version.
if (id === undefined) {
return client.postNewAddon(uploadUuid, savedIdPath, metaDataJson);
return client.postNewAddon(
uploadUuid,
savedIdPath,
metaDataJson,
versionPatchData,
);
}

return client.putVersion(uploadUuid, id, metaDataJson);
return client.putVersion(uploadUuid, id, metaDataJson, versionPatchData);
}

export async function saveIdToFile(filePath, id) {
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test-cmd/test.sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,23 @@ describe('sign', () => {
});
}));

it('passes the versionSource parameter to submissionAPI signer', () =>
withTempDir((tmpDir) => {
const stubs = getStubs();
const versionSource = 'path/to/source.zip';
return sign(tmpDir, stubs, {
extraArgs: {
versionSource,
useSubmissionApi: true,
channel: 'unlisted',
},
}).then(() => {
sinon.assert.called(stubs.signingOptions.submitAddon);
sinon.assert.calledWithMatch(stubs.signingOptions.submitAddon, {
versionSource,
});
});
}));
it('returns a signing result', () =>
withTempDir((tmpDir) => {
const stubs = getStubs();
Expand Down
173 changes: 172 additions & 1 deletion tests/unit/test-util/test.submit-addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ describe('util.submit-addon', () => {
let getPreviousUuidOrUploadXpiStub;
let postNewAddonStub;
let putVersionStub;
let fileFromSyncStub;
const uploadUuid = '{some-upload-uuid}';
const fakeFileFromSync = new File([], 'foo.xpi');

beforeEach(() => {
statStub = sinon
Expand All @@ -61,13 +63,17 @@ describe('util.submit-addon', () => {
.resolves(uploadUuid);
postNewAddonStub = sinon.stub(Client.prototype, 'postNewAddon');
putVersionStub = sinon.stub(Client.prototype, 'putVersion');
fileFromSyncStub = sinon
.stub(Client.prototype, 'fileFromSync')
.returns(fakeFileFromSync);
});

afterEach(() => {
statStub.restore();
getPreviousUuidOrUploadXpiStub.restore();
postNewAddonStub.restore();
putVersionStub.restore();
fileFromSyncStub.restore();
});

const signAddonDefaults = {
Expand Down Expand Up @@ -122,6 +128,7 @@ describe('util.submit-addon', () => {
downloadDir,
userAgentString,
});
sinon.assert.notCalled(fileFromSyncStub);
});

it('calls postNewAddon if `id` is undefined', async () => {
Expand Down Expand Up @@ -187,6 +194,42 @@ describe('util.submit-addon', () => {
metaDataJson,
);
});

it('includes source data to be patched if versionSource defined for new addon', async () => {
const versionSource = 'path/to/source/zip';
await signAddon({
...signAddonDefaults,
versionSource,
});

sinon.assert.calledWith(fileFromSyncStub, versionSource);
sinon.assert.calledWith(
postNewAddonStub,
uploadUuid,
signAddonDefaults.savedIdPath,
{},
{ source: fakeFileFromSync },
);
});

it('includes source data to be patched if versionSource defined for new version', async () => {
const versionSource = 'path/to/source/zip';
const id = '@thisID';
await signAddon({
...signAddonDefaults,
versionSource,
id,
});

sinon.assert.calledWith(fileFromSyncStub, versionSource);
sinon.assert.calledWith(
putVersionStub,
uploadUuid,
id,
{},
{ source: fakeFileFromSync },
);
});
});

describe('Client', () => {
Expand Down Expand Up @@ -738,6 +781,42 @@ describe('util.submit-addon', () => {
});
});

describe('doFormDataPatch', () => {
const addonId = 'some-addon-id';
const versionId = 123456;
const dataField1 = 'someField';
const dataField2 = 'otherField';
const data = { dataField1: 'value', dataField2: 0 };
const formData = new FormData();
formData.append(dataField1, data[dataField1]);
formData.append(dataField2, data[dataField2]);

it('creates the url from addon and version', async () => {
const client = new Client(clientDefaults);
const fetchStub = sinon
.stub(client, 'fetch')
.resolves(new Response('', { ok: true, status: 200 }));
await client.doFormDataPatch(data, addonId, versionId);
const patchUrl = new URL(
`addon/${addonId}/versions/${versionId}/`,
client.apiUrl,
);

sinon.assert.calledWith(fetchStub, patchUrl, 'PATCH', formData);
});

it('catches and throws for non ok responses', async () => {
const client = new Client(clientDefaults);
sinon.stub(client, 'fetch').resolves();
const response = client.doFormDataPatch(data, addonId, versionId);

assert.isRejected(
response,
`Uploading ${dataField1}${dataField2} failed`,
);
});
});

describe('waitForApproval', () => {
it('aborts approval wait after timeout', async () => {
const client = new Client({
Expand Down Expand Up @@ -902,7 +981,13 @@ describe('util.submit-addon', () => {
[{ body: sampleAddonDetail, status: 200 }],
);
addApprovalMocks(versionId);
await client.postNewAddon(uploadUuid, idFile, {}, saveIdStub);
await client.postNewAddon(
uploadUuid,
idFile,
{},
undefined,
saveIdStub,
);
sinon.assert.calledWith(saveIdStub, idFile, sampleAddonDetail.guid);
});

Expand All @@ -920,6 +1005,92 @@ describe('util.submit-addon', () => {
await client.putVersion(uploadUuid, `${addonId}`, {});
});

describe('doFormDataPatch called correctly', () => {
const versionPatchData = { source: 'somesource' };
const metaDataJson = { some: 'metadata' };
const newVersionId = 123456;
const editUrl = 'http://some/url';
const stubbedClient = new Client(clientDefaults);

const submitResponse = {
guid: addonId,
version: { id: newVersionId, edit_url: editUrl },
};
sinon
.stub(stubbedClient, 'doNewAddonOrVersionSubmit')
.resolves(submitResponse);
sinon.stub(stubbedClient, 'doNewAddonSubmit').resolves(submitResponse);
sinon.stub(stubbedClient, 'doAfterSubmit').resolves();

let doFormDataPatchStub;

before(() => {
doFormDataPatchStub = sinon
.stub(stubbedClient, 'doFormDataPatch')
.resolves();
});

afterEach(() => {
doFormDataPatchStub.reset();
});

it('calls doFormDataPatch if versionPatchData is defined for postNewAddon', async () => {
const saveIdToFileStub = sinon.stub().resolves();
const savedIdPath = 'some/saved/id/path';
await stubbedClient.postNewAddon(
uploadUuid,
savedIdPath,
metaDataJson,
versionPatchData,
saveIdToFileStub,
);

sinon.assert.calledWith(
doFormDataPatchStub,
versionPatchData,
addonId,
newVersionId,
);
});

it('calls doFormDataPatch if versionPatchData is defined for putVersion', async () => {
await stubbedClient.putVersion(
uploadUuid,
addonId,
metaDataJson,
versionPatchData,
);

sinon.assert.called(doFormDataPatchStub);
sinon.assert.calledWith(
doFormDataPatchStub,
versionPatchData,
addonId,
newVersionId,
);
});

it('does not call doFormDataPatch is versionPatchData is undefined for postNewAddon', async () => {
const saveIdToFileStub = sinon.stub().resolves();
const savedIdPath = 'some/saved/id/path';
await stubbedClient.postNewAddon(
uploadUuid,
savedIdPath,
metaDataJson,
undefined,
saveIdToFileStub,
);

sinon.assert.notCalled(doFormDataPatchStub);
});

it('does not call doFormDataPatch is versionPatchData is undefined for putVersion', async () => {
await stubbedClient.putVersion(uploadUuid, addonId, metaDataJson);

sinon.assert.notCalled(doFormDataPatchStub);
});
});

describe('doAfterSubmit', () => {
const downloadUrl = 'https://a.download/url';
let approvalStub;
Expand Down