Fix auth connection (#15869)

Better to do auth this way than in module
This commit is contained in:
Félix Malfait
2025-11-17 18:12:48 +01:00
committed by GitHub
parent 8d7ee47ec5
commit 19ca93b954
9 changed files with 58 additions and 39 deletions
@@ -1,5 +1,7 @@
import { Injectable, Logger } from '@nestjs/common';
import { google } from 'googleapis';
import {
MessageFolder,
MessageFolderDriver,
@@ -39,7 +41,10 @@ export class GmailGetAllFoldersService implements MessageFolderDriver {
connectedAccount,
);
const gmailClient = oAuth2Client.gmail({ version: 'v1' });
const gmailClient = google.gmail({
version: 'v1',
auth: oAuth2Client,
});
const response = await gmailClient.users.labels
.list({ userId: 'me' })
@@ -1,5 +1,6 @@
import { Test, type TestingModule } from '@nestjs/testing';
import { google } from 'googleapis';
import { ConnectedAccountProvider } from 'twenty-shared/types';
import { OAuth2ClientManagerService } from 'src/modules/connected-account/oauth2-client-manager/services/oauth2-client-manager.service';
@@ -63,6 +64,10 @@ describe('GmailGetMessageListService', () => {
);
});
afterEach(() => {
jest.restoreAllMocks();
});
describe('getMessageList', () => {
it('should return 0 messageExternalIds when gmail returns 0 messages', async () => {
const mockGmailClient = {
@@ -78,13 +83,11 @@ describe('GmailGetMessageListService', () => {
},
};
const mockOAuth2Client = {
gmail: jest.fn().mockReturnValue(mockGmailClient),
};
jest.spyOn(google, 'gmail').mockReturnValue(mockGmailClient as never);
(
oAuth2ClientManagerService.getGoogleOAuth2Client as jest.Mock
).mockResolvedValue(mockOAuth2Client);
).mockResolvedValue({});
const result = await service.getMessageLists({
messageChannel: { syncCursor: '', id: 'my-id' },
@@ -133,13 +136,11 @@ describe('GmailGetMessageListService', () => {
},
};
const mockOAuth2Client = {
gmail: jest.fn().mockReturnValue(mockGmailClient),
};
jest.spyOn(google, 'gmail').mockReturnValue(mockGmailClient as never);
(
oAuth2ClientManagerService.getGoogleOAuth2Client as jest.Mock
).mockResolvedValue(mockOAuth2Client);
).mockResolvedValue({});
const result = await service.getMessageLists({
messageChannel: { syncCursor: '', id: 'my-id' },
@@ -188,13 +189,11 @@ describe('GmailGetMessageListService', () => {
},
};
const mockOAuth2Client = {
gmail: jest.fn().mockReturnValue(mockGmailClient),
};
jest.spyOn(google, 'gmail').mockReturnValue(mockGmailClient as never);
(
oAuth2ClientManagerService.getGoogleOAuth2Client as jest.Mock
).mockResolvedValue(mockOAuth2Client);
).mockResolvedValue({});
const result = await service.getMessageLists({
messageChannel: { syncCursor: '', id: 'my-id' },
@@ -224,13 +223,11 @@ describe('GmailGetMessageListService', () => {
},
};
const mockOAuth2Client = {
gmail: jest.fn().mockReturnValue(mockGmailClient),
};
jest.spyOn(google, 'gmail').mockReturnValue(mockGmailClient as never);
(
oAuth2ClientManagerService.getGoogleOAuth2Client as jest.Mock
).mockResolvedValue(mockOAuth2Client);
).mockResolvedValue({});
const result = await service.getMessageLists({
messageChannel: { syncCursor: '', id: 'my-id' },
@@ -1,7 +1,7 @@
import { Injectable, Logger } from '@nestjs/common';
import { isNonEmptyString } from '@sniptt/guards';
import { type gmail_v1 as gmailV1 } from 'googleapis';
import { google, type gmail_v1 as gmailV1 } from 'googleapis';
import { isDefined } from 'twenty-shared/utils';
import { OAuth2ClientManagerService } from 'src/modules/connected-account/oauth2-client-manager/services/oauth2-client-manager.service';
@@ -42,7 +42,10 @@ export class GmailGetMessageListService {
await this.oAuth2ClientManagerService.getGoogleOAuth2Client(
connectedAccount,
);
const gmailClient = oAuth2Client.gmail({ version: 'v1' });
const gmailClient = google.gmail({
version: 'v1',
auth: oAuth2Client,
});
let pageToken: string | undefined;
let hasMoreMessages = true;
@@ -143,7 +146,10 @@ export class GmailGetMessageListService {
await this.oAuth2ClientManagerService.getGoogleOAuth2Client(
connectedAccount,
);
const gmailClient = oAuth2Client.gmail({ version: 'v1' });
const gmailClient = google.gmail({
version: 'v1',
auth: oAuth2Client,
});
if (!isNonEmptyString(messageChannel.syncCursor)) {
return this.getMessageListWithoutCursor(connectedAccount, messageFolders);
@@ -1,5 +1,6 @@
import { Test, type TestingModule } from '@nestjs/testing';
import { google } from 'googleapis';
import { ConnectedAccountProvider } from 'twenty-shared/types';
import { OAuth2ClientManagerService } from 'src/modules/connected-account/oauth2-client-manager/services/oauth2-client-manager.service';
@@ -27,7 +28,7 @@ describe('MessagingSendMessageService - Gmail HTML Support', () => {
},
getProfile: jest
.fn()
.mockResolvedValue({ data: { emailAdress: 'test@example.com' } }),
.mockResolvedValue({ data: { emailAddress: 'test@example.com' } }),
},
};
@@ -45,12 +46,12 @@ describe('MessagingSendMessageService - Gmail HTML Support', () => {
},
};
const mockOAuth2Client = {
gmail: jest.fn().mockReturnValue(mockGmailClient),
people: jest.fn().mockReturnValue(mockPeopleClient),
};
const mockOAuth2Client = {};
beforeEach(async () => {
jest.spyOn(google, 'gmail').mockReturnValue(mockGmailClient as never);
jest.spyOn(google, 'people').mockReturnValue(mockPeopleClient as never);
const module: TestingModule = await Test.createTestingModule({
providers: [
MessagingSendMessageService,
@@ -79,7 +80,8 @@ describe('MessagingSendMessageService - Gmail HTML Support', () => {
});
afterEach(() => {
jest.clearAllMocks();
mockSend.mockClear();
jest.restoreAllMocks();
});
it('should send multipart/alternative email with both text and HTML parts via Gmail', async () => {
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common';
import { google } from 'googleapis';
import MailComposer from 'nodemailer/lib/mail-composer';
import { ConnectedAccountProvider } from 'twenty-shared/types';
import { assertUnreachable, isDefined } from 'twenty-shared/utils';
@@ -47,12 +48,14 @@ export class MessagingSendMessageService {
connectedAccount,
);
const gmailClient = oAuth2Client.gmail({
const gmailClient = google.gmail({
version: 'v1',
auth: oAuth2Client,
});
const peopleClient = oAuth2Client.people({
const peopleClient = google.people({
version: 'v1',
auth: oAuth2Client,
});
const { data: gmailData } = await gmailClient.users.getProfile({