-
Notifications
You must be signed in to change notification settings - Fork 1
Use resource id instead of document name #14
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,18 +24,17 @@ describe("OpenProjectApi", () => { | |
| await expect(() => | ||
| new OpenProjectApi().onAuthenticate({ | ||
| token: null, | ||
| requestParameters: new URLSearchParams({ document_id: "121", openproject_base_path: "https://test.api" }), | ||
| } as unknown as onAuthenticatePayload) | ||
| ).rejects.toThrowError("Unauthorized: Token missing."); | ||
| }); | ||
|
|
||
| test("when the opBasePath is not present throw an error", async () => { | ||
| test("when the token is invalid", async () => { | ||
| await expect(() => | ||
| new OpenProjectApi().onAuthenticate({ | ||
| token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==", | ||
| requestParameters: new URLSearchParams({ document_id: "121" }), | ||
| // Invalid token, generated with a different secret | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for adding this comment! |
||
| token: "5Sm4blMLhP8PFS67xw==--br8L/7YDX3rbTLpT--HHEi+SnNdmHmH90N3mHY9A==", | ||
| } as unknown as onAuthenticatePayload) | ||
| ).rejects.toThrowError("Unauthorized: Base URL missing."); | ||
| ).rejects.toThrowError("Unsupported state or unable to authenticate data"); | ||
| }); | ||
|
|
||
| test("when ALLOWED_DOMAINS is not configured throw an error", async () => { | ||
|
|
@@ -44,58 +43,45 @@ describe("OpenProjectApi", () => { | |
| await expect(() => | ||
| new OpenProjectApi().onAuthenticate({ | ||
| token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==", | ||
| requestParameters: new URLSearchParams({ document_id: "121", openproject_base_path: "https://test.api" }), | ||
| } as unknown as onAuthenticatePayload) | ||
| ).rejects.toThrowError("Unauthorized: No allowed domains configured."); | ||
| }); | ||
|
|
||
| test("when opBasePath has invalid format throw an error", async () => { | ||
| test("when the resourceUrl has invalid format throw an error", async () => { | ||
| await expect(() => | ||
| new OpenProjectApi().onAuthenticate({ | ||
| token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==", | ||
| requestParameters: new URLSearchParams({ document_id: "121", openproject_base_path: "not-a-valid-url" }), | ||
| documentName: "not a valid url", | ||
| } as unknown as onAuthenticatePayload) | ||
| ).rejects.toThrowError("Unauthorized: Invalid base URL format."); | ||
| }); | ||
|
|
||
| test("when opBasePath domain is not in ALLOWED_DOMAINS throw an error", async () => { | ||
| test("when the resourceUrl domain is not in ALLOWED_DOMAINS throw an error", async () => { | ||
| await expect(() => | ||
| new OpenProjectApi().onAuthenticate({ | ||
| token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==", | ||
| requestParameters: new URLSearchParams({ document_id: "121", openproject_base_path: "https://malicious.com" }), | ||
| documentName: "https://malicious.com/something/1", | ||
| } as unknown as onAuthenticatePayload) | ||
| ).rejects.toThrowError("Unauthorized: Invalid base URL domain."); | ||
| }); | ||
|
|
||
| test("when opBasePath subdomain matches ALLOWED_DOMAINS it should be accepted", async () => { | ||
| test("when the resourceUrl subdomain matches ALLOWED_DOMAINS it should be accepted", async () => { | ||
| fetchMock.mockResolvedValueOnce({ | ||
| ok: true, | ||
| status: 200, | ||
| json: () => Promise.resolve({ title: "TheDocName" }), | ||
| json: () => Promise.resolve({}), | ||
| }); | ||
|
|
||
| const data = { | ||
| context: {}, | ||
| connectionConfig: {}, | ||
| token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==", | ||
| documentName: "TheDocName", | ||
| requestParameters: new URLSearchParams({ document_id: "121", openproject_base_path: "https://subdomain.test.api" }), | ||
| documentName: "https://subdomain.test.api/api/v3/documents/1", | ||
| } as unknown as onAuthenticatePayload; | ||
|
|
||
| await new OpenProjectApi().onAuthenticate(data); | ||
|
|
||
| expect(data.context.opBasePath).toEqual("https://subdomain.test.api"); | ||
| }); | ||
|
|
||
| test("when the token is invalid", async () => { | ||
| await expect(() => | ||
| new OpenProjectApi().onAuthenticate({ | ||
| // Invalid token, generated with a different secret | ||
| token: "5Sm4blMLhP8PFS67xw==--br8L/7YDX3rbTLpT--HHEi+SnNdmHmH90N3mHY9A==", | ||
| documentName: "TheDocName", | ||
| requestParameters: new URLSearchParams({ document_id: "121", openproject_base_path: "https://test.api" }), | ||
| } as unknown as onAuthenticatePayload) | ||
| ).rejects.toThrowError("Unsupported state or unable to authenticate data"); | ||
| expect(data.context.resourceUrl).toEqual("https://subdomain.test.api/api/v3/documents/1"); | ||
| }); | ||
|
|
||
| test("when the server does not authorize the request throw an error", async () => { | ||
|
|
@@ -107,10 +93,9 @@ describe("OpenProjectApi", () => { | |
| await expect(() => | ||
| new OpenProjectApi().onAuthenticate({ | ||
| token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==", | ||
| documentName: "TheDocName", | ||
| requestParameters: new URLSearchParams({ document_id: "121", openproject_base_path: "https://test.api" }), | ||
| documentName: "https://test.api/api/v3/documents/121", | ||
| } as unknown as onAuthenticatePayload) | ||
| ).rejects.toThrowError("Unauthorized: Invalid token."); | ||
| ).rejects.toThrowError("Unauthorized: Invalid token or document access denied."); | ||
|
|
||
| expect(fetchMock).toHaveBeenCalledWith( | ||
| "https://test.api/api/v3/documents/121", | ||
|
|
@@ -124,51 +109,32 @@ describe("OpenProjectApi", () => { | |
| ); | ||
| }); | ||
|
|
||
| test("when the document title does not match the requested documentName, throw an error", async () => { | ||
| test("when the token is valid set the context", async () => { | ||
| fetchMock.mockResolvedValueOnce({ | ||
| ok: true, | ||
| status: 200, | ||
| json: () => Promise.resolve({ title: "DifferentDocName" }), | ||
| }); | ||
|
|
||
| await expect(() => | ||
| new OpenProjectApi().onAuthenticate({ | ||
| token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==", | ||
| documentName: "TheDocName", | ||
| requestParameters: new URLSearchParams({ document_id: "121", openproject_base_path: "https://test.api" }), | ||
| } as unknown as onAuthenticatePayload) | ||
| ).rejects.toThrowError("Unauthorized: Document access denied."); | ||
| }); | ||
|
|
||
| test("when the token is valid and document title matches, set the document_id, token and opBasePath on the context", async () => { | ||
| fetchMock.mockResolvedValueOnce({ | ||
| ok: true, | ||
| status: 200, | ||
| json: () => Promise.resolve({ title: "TheDocName" }), | ||
| json: () => Promise.resolve({}), | ||
| }); | ||
|
|
||
| const data = { | ||
| context: {}, | ||
| connectionConfig: {}, | ||
| token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==", | ||
| documentName: "TheDocName", | ||
| requestParameters: new URLSearchParams({ document_id: "121", openproject_base_path: "https://test.api" }), | ||
| documentName: "https://test.api/api/v3/documents/121", | ||
| } as unknown as onAuthenticatePayload; | ||
|
|
||
| await new OpenProjectApi().onAuthenticate(data); | ||
|
|
||
| expect(data.context.documentId).toEqual("121"); | ||
| expect(data.context.resourceUrl).toEqual("https://test.api/api/v3/documents/121"); | ||
| expect(data.context.token).toEqual("valid_token"); | ||
| expect(data.context.opBasePath).toEqual("https://test.api"); | ||
| expect(data.documentName).toEqual("TheDocName"); | ||
| expect(data.documentName).toEqual("https://test.api/api/v3/documents/121"); | ||
| }); | ||
|
|
||
| test("when there is no update link, setup the connection as readonly", async () => { | ||
| fetchMock.mockResolvedValueOnce({ | ||
| ok: true, | ||
| status: 200, | ||
| json: () => Promise.resolve({ | ||
| title: "TheDocName", | ||
| _links: { | ||
| self: { href: "/api/v3/documents/121" } | ||
| } | ||
|
|
@@ -179,8 +145,7 @@ describe("OpenProjectApi", () => { | |
| context: {}, | ||
| connectionConfig: {}, | ||
| token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==", | ||
| documentName: "TheDocName", | ||
| requestParameters: new URLSearchParams({ document_id: "121", openproject_base_path: "https://test.api" }), | ||
| documentName: "https://test.api/api/v3/documents/121", | ||
| } as unknown as onAuthenticatePayload; | ||
|
|
||
| await new OpenProjectApi().onAuthenticate(data); | ||
|
|
@@ -206,8 +171,7 @@ describe("OpenProjectApi", () => { | |
| context: {}, | ||
| connectionConfig: {}, | ||
| token: "7u+b+QRJN7qANls=--URNw83hIWBq3MMIA--jtl+UPdtbniQVFNOs2EcAw==", | ||
| documentName: "TheDocName", | ||
| requestParameters: new URLSearchParams({ document_id: "121", openproject_base_path: "https://test.api" }), | ||
| documentName: "https://test.api/api/v3/documents/121", | ||
| } as unknown as onAuthenticatePayload; | ||
|
|
||
| await new OpenProjectApi().onAuthenticate(data); | ||
|
|
@@ -228,14 +192,12 @@ describe("OpenProjectApi", () => { | |
| fetchMock.mockResolvedValueOnce({ | ||
| ok: true, | ||
| status: 200, | ||
| json: () => Promise.resolve({ | ||
| contentBinary: base64Update | ||
| }), | ||
| json: () => Promise.resolve({ contentBinary: base64Update }), | ||
| }); | ||
|
|
||
| const targetDoc = new Y.Doc(); | ||
| const data = { | ||
| context: { documentId: "121", token: "testToken", opBasePath: "https://test.api" }, | ||
| context: { token: "superValidToken", resourceUrl: "https://test.api/api/v3/documents/121" }, | ||
| document: targetDoc, | ||
| } as onLoadDocumentPayload; | ||
|
|
||
|
|
@@ -248,7 +210,7 @@ describe("OpenProjectApi", () => { | |
| method: "GET", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| "Authorization": expect.stringContaining("Bearer"), | ||
| "Authorization": "Bearer superValidToken", | ||
| }, | ||
| } | ||
| ); | ||
|
|
@@ -265,7 +227,7 @@ describe("OpenProjectApi", () => { | |
| }); | ||
|
|
||
| const data = { | ||
| context: { documentId: "121", token: "testToken", opBasePath: "https://test.api" }, | ||
| context: { token: "superValidToken", resourceUrl: "https://test.api/api/v3/documents/121" }, | ||
| document: new Y.Doc(), | ||
| } as onLoadDocumentPayload; | ||
|
|
||
|
|
@@ -305,10 +267,9 @@ describe("OpenProjectApi", () => { | |
|
|
||
| const data = { | ||
| context: { | ||
| documentId: "121", | ||
| token: "testToken", | ||
| opBasePath: "https://test.api", | ||
| readonly: false | ||
| token: "superValidToken", | ||
| resourceUrl: "https://test.api/api/v3/documents/121", | ||
| readonly: false, | ||
| }, | ||
| document, | ||
| } as onStoreDocumentPayload; | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is so much more elegant 😍