Last active
August 10, 2021 19:11
-
-
Save ElpixZero/210b7bcbb59cf879c6b91599e0fb9c1e to your computer and use it in GitHub Desktop.
This file contains 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
const { shareDocumentWithStudents } = require("../sharer.js"); | |
const mockLiqpayApi = jest | |
.fn() | |
.mockImplementation((_, __, resolve) => resolve({ data: [] })); | |
jest.mock("../liqpay.js", () => | |
jest.fn().mockImplementation(function mockLiqPay() { | |
this.api = mockLiqpayApi; | |
}) | |
); | |
const mockGoogleDrivePermissionsList = jest.fn().mockResolvedValue({ | |
data: { permissions: [] }, | |
}); | |
let mockGoogleDrivePermissionsCreate = jest.fn(); | |
jest.mock("googleapis", () => ({ | |
google: { | |
auth: { | |
JWT: jest.fn().mockImplementation(function mockJWT() { | |
this.authorize = jest.fn(); | |
this.credentials = { access_token: "" }; | |
}), | |
}, | |
drive: jest.fn().mockImplementation(() => ({ | |
permissions: { | |
list: mockGoogleDrivePermissionsList, | |
create: mockGoogleDrivePermissionsCreate, | |
}, | |
})), | |
}, | |
})); | |
describe("Sharer", () => { | |
// + Если не оплатил и есть в списке -> ничего не делать | |
// + Если не оплатил и нету в списке -> ничего не делать | |
const FAKE_FILE_ID = "FAKE_FILE_ID"; | |
const EXPECTED_DESCRIPTION = "Мастер-класс по Unit-тестированию JS"; | |
describe("when person paid but is not in enrolled list", () => { | |
const EMAIL = "some@email"; | |
beforeEach(() => { | |
mockLiqpayApi.mockImplementation((_, __, resolve) => | |
resolve({ | |
data: [ | |
{ | |
description: EXPECTED_DESCRIPTION, | |
order_id: `${EMAIL} /// Something`, | |
status: "success", | |
}, | |
], | |
}) | |
); | |
mockGoogleDrivePermissionsList.mockResolvedValue({ | |
data: { permissions: [{ emailAddress: "some_other@email" }] }, | |
}); | |
}); | |
it("requests commenter access from google drive", async () => { | |
await shareDocumentWithStudents(FAKE_FILE_ID); | |
expect(mockGoogleDrivePermissionsCreate).toHaveBeenCalledWith( | |
expect.objectContaining({ | |
fileId: FAKE_FILE_ID, | |
emailAddress: EMAIL, | |
resource: expect.objectContaining({ | |
emailAddress: EMAIL, | |
type: "user", | |
role: "commenter", | |
}), | |
}) | |
); | |
}); | |
it("writes to console.log on success sharing", async () => { | |
const consoleLogSpy = jest.spyOn(console, "log"); | |
await shareDocumentWithStudents(FAKE_FILE_ID); | |
expect(consoleLogSpy).toHaveBeenCalledWith( | |
expect.stringContaining(EMAIL) | |
); | |
}); | |
it("throws an error if sharing fails", async () => { | |
mockGoogleDrivePermissionsCreate.mockRejectedValueOnce( | |
new Error("Unknown error") | |
); | |
expect(shareDocumentWithStudents(FAKE_FILE_ID)).rejects.toBeInstanceOf( | |
Error | |
); | |
}); | |
it("does not reports email if sharing fails", async () => { | |
const consoleLogSpy = jest.spyOn(console, "log"); | |
mockGoogleDrivePermissionsCreate.mockRejectedValueOnce( | |
new Error("Unknown error") | |
); | |
await shareDocumentWithStudents(FAKE_FILE_ID).catch(() => {}); | |
expect(consoleLogSpy).not.toHaveBeenCalledWith( | |
expect.stringContaining(EMAIL) | |
); | |
}); | |
}); | |
describe("when person paid and is in enrolled list", () => { | |
const EMAIL = "some@email"; | |
beforeEach(() => { | |
mockLiqpayApi.mockImplementation((_, __, resolve) => | |
resolve({ | |
data: [ | |
{ | |
description: EXPECTED_DESCRIPTION, | |
order_id: `${EMAIL} /// Something`, | |
status: "success", | |
}, | |
], | |
}) | |
); | |
mockGoogleDrivePermissionsList.mockResolvedValue({ | |
data: { permissions: [{ emailAddress: EMAIL }] }, | |
}); | |
}); | |
it("does not attempts to share with this email", async () => { | |
await shareDocumentWithStudents(FAKE_FILE_ID); | |
expect(mockGoogleDrivePermissionsCreate).not.toHaveBeenCalledWith( | |
expect.objectContaining({ | |
emailAddress: EMAIL, | |
}) | |
); | |
}); | |
}); | |
describe("when person didn't pay and is in enrolled list", () => { | |
const EMAIL = "some@email"; | |
beforeEach(() => { | |
mockLiqpayApi.mockImplementation((_, __, resolve) => | |
resolve({ | |
data: [ | |
{ | |
description: EXPECTED_DESCRIPTION, | |
order_id: `some_another@mail /// Something`, | |
status: "success", | |
}, | |
], | |
}) | |
); | |
mockGoogleDrivePermissionsList.mockResolvedValue({ | |
data: { permissions: [{ emailAddress: EMAIL }] }, | |
}); | |
}); | |
it("does not attempt to share with this email", async () => { | |
await shareDocumentWithStudents(FAKE_FILE_ID); | |
expect(mockGoogleDrivePermissionsCreate).not.toHaveBeenCalledWith( | |
expect.objectContaining({ | |
emailAddress: EMAIL, | |
}) | |
); | |
}); | |
}); | |
describe("when person didn't pay and isn't in enrolled list", () => { | |
const EMAIL = "some@email"; | |
beforeEach(() => { | |
mockLiqpayApi.mockImplementation((_, __, resolve) => | |
resolve({ | |
data: [ | |
{ | |
description: EXPECTED_DESCRIPTION, | |
order_id: `some_another@mail /// Something`, | |
status: "success", | |
}, | |
], | |
}) | |
); | |
mockGoogleDrivePermissionsList.mockResolvedValue({ | |
data: { permissions: [{ emailAddress: "some_another2@mail" }] }, | |
}); | |
}); | |
it("does not attempt to share with this email", async () => { | |
await shareDocumentWithStudents(FAKE_FILE_ID); | |
expect(mockGoogleDrivePermissionsCreate).not.toHaveBeenCalledWith( | |
expect.objectContaining({ | |
emailAddress: EMAIL, | |
}) | |
); | |
}); | |
}); | |
describe("when persons payment doesn't have correct description", () => { | |
const EMAIL = "some@email"; | |
beforeEach(() => { | |
mockLiqpayApi.mockImplementation((_, __, resolve) => | |
resolve({ | |
data: [ | |
{ | |
description: "test", | |
order_id: `${EMAIL} /// Something`, | |
status: "success", | |
}, | |
], | |
}) | |
); | |
mockGoogleDrivePermissionsList.mockResolvedValue({ | |
data: { permissions: [{ emailAddress: "some_another@mail" }] }, | |
}); | |
}); | |
it("does not attempt to share with this email", async () => { | |
await shareDocumentWithStudents(FAKE_FILE_ID); | |
expect(mockGoogleDrivePermissionsCreate).not.toHaveBeenCalledWith( | |
expect.objectContaining({ | |
emailAddress: EMAIL, | |
}) | |
); | |
}); | |
}); | |
describe("when persons payment doesn't have correct order_id", () => { | |
const EMAIL = "some@email"; | |
beforeEach(() => { | |
mockLiqpayApi.mockImplementation((_, __, resolve) => | |
resolve({ | |
data: [ | |
{ | |
description: EXPECTED_DESCRIPTION, | |
order_id: `${EMAIL} test order_id`, | |
status: "success", | |
}, | |
], | |
}) | |
); | |
mockGoogleDrivePermissionsList.mockResolvedValue({ | |
data: { permissions: [{ emailAddress: "some_another@mail" }] }, | |
}); | |
}); | |
it("does not attempt to share with this email", async () => { | |
await shareDocumentWithStudents(FAKE_FILE_ID); | |
expect(mockGoogleDrivePermissionsCreate).not.toHaveBeenCalledWith( | |
expect.objectContaining({ | |
emailAddress: EMAIL, | |
}) | |
); | |
}); | |
}); | |
describe("when persons payment doesn't have success status", () => { | |
const EMAIL = "some@email"; | |
beforeEach(() => { | |
mockLiqpayApi.mockImplementation((_, __, resolve) => | |
resolve({ | |
data: [ | |
{ | |
description: EXPECTED_DESCRIPTION, | |
order_id: `${EMAIL} /// Something`, | |
}, | |
], | |
}) | |
); | |
mockGoogleDrivePermissionsList.mockResolvedValue({ | |
data: { permissions: [{ emailAddress: "some_another@mail" }] }, | |
}); | |
}); | |
it("does not attempt to share with this email", async () => { | |
await shareDocumentWithStudents(FAKE_FILE_ID); | |
expect(mockGoogleDrivePermissionsCreate).not.toHaveBeenCalledWith( | |
expect.objectContaining({ | |
emailAddress: EMAIL, | |
}) | |
); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment