// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import DatabaseManager from '@database/admin/database_manager'; import {DatabaseType, OperationType} from '@typings/database/enums'; import { operateAppRecord, operateChannelInfoRecord, operateChannelMembershipRecord, operateChannelRecord, operateCustomEmojiRecord, operateDraftRecord, operateFileRecord, operateGlobalRecord, operateGroupMembershipRecord, operateGroupRecord, operateGroupsInChannelRecord, operateGroupsInTeamRecord, operateMyChannelRecord, operateMyChannelSettingsRecord, operateMyTeamRecord, operatePostInThreadRecord, operatePostMetadataRecord, operatePostRecord, operatePostsInChannelRecord, operatePreferenceRecord, operateReactionRecord, operateRoleRecord, operateServersRecord, operateSlashCommandRecord, operateSystemRecord, operateTeamChannelHistoryRecord, operateTeamMembershipRecord, operateTeamRecord, operateTeamSearchHistoryRecord, operateTermsOfServiceRecord, operateUserRecord, } from './index'; jest.mock('@database/admin/database_manager'); /* eslint-disable @typescript-eslint/no-explicit-any */ describe('*** DataOperator: Operators tests ***', () => { const createConnection = async (setActive = false) => { const dbName = 'server_schema_connection'; const serverUrl = 'https://appv2.mattermost.com'; const database = await DatabaseManager.createDatabaseConnection({ shouldAddToDefaultDatabase: true, configs: { actionsEnabled: true, dbName, dbType: DatabaseType.SERVER, serverUrl, }, }); if (setActive) { await DatabaseManager.setActiveServerDatabase({ displayName: dbName, serverUrl, }); } return database; }; it('=> operateAppRecord: should return an array of type App', async () => { expect.assertions(3); const database = await DatabaseManager.getDefaultDatabase(); expect(database).toBeTruthy(); const preparedRecords = await operateAppRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { build_number: 'build-7', created_at: 1, version_number: 'v-1', }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('App'); }); it('=> operateGlobalRecord: should return an array of type Global', async () => { expect.assertions(3); const database = await DatabaseManager.getDefaultDatabase(); expect(database).toBeTruthy(); const preparedRecords = await operateGlobalRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: {name: 'g-n1', value: 'g-v1'}, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('Global'); }); it('=> operateServersRecord: should return an array of type Servers', async () => { expect.assertions(3); const database = await DatabaseManager.getDefaultDatabase(); expect(database).toBeTruthy(); const preparedRecords = await operateServersRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { db_path: 'mm-server', display_name: 's-displayName', mention_count: 1, unread_count: 0, url: 'https://community.mattermost.com', }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('Servers'); }); it('=> operateRoleRecord: should return an array of type Role', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateRoleRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { id: 'role-1', name: 'role-name-1', permissions: [], }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('Role'); }); it('=> operateSystemRecord: should return an array of type System', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateSystemRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: {id: 'system-1', name: 'system-name-1', value: 'system'}, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('System'); }); it('=> operateTermsOfServiceRecord: should return an array of type TermsOfService', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateTermsOfServiceRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { id: 'tos-1', accepted_at: 1, create_at: 1613667352029, user_id: 'user1613667352029', text: '', }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe( 'TermsOfService', ); }); it('=> operatePostRecord: should return an array of type Post', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operatePostRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { id: '8swgtrrdiff89jnsiwiip3y1eoe', create_at: 1596032651748, update_at: 1596032651748, edit_at: 0, delete_at: 0, is_pinned: false, user_id: 'q3mzxua9zjfczqakxdkowc6u6yy', channel_id: 'xxoq1p6bqg7dkxb3kj1mcjoungw', root_id: 'ps81iqbesfby8jayz7owg4yypoo', parent_id: 'ps81iqbddesfby8jayz7owg4yypoo', original_id: '', message: 'Testing operator post', type: '', props: {}, hashtags: '', pending_post_id: '', reply_count: 4, last_reply_at: 0, participants: null, metadata: {}, }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('Post'); }); it('=> operatePostInThreadRecord: should return an array of type PostsInThread', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operatePostInThreadRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { id: 'ps81iqbddesfby8jayz7owg4yypoo', post_id: '8swgtrrdiff89jnsiwiip3y1eoe', earliest: 1596032651748, latest: 1597032651748, }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe( 'PostsInThread', ); }); it('=> operateReactionRecord: should return an array of type Reaction', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateReactionRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { id: 'ps81iqbddesfby8jayz7owg4yypoo', user_id: 'q3mzxua9zjfczqakxdkowc6u6yy', post_id: 'ps81iqbddesfby8jayz7owg4yypoo', emoji_name: 'thumbsup', create_at: 1596032651748, update_at: 1608253011321, delete_at: 0, }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('Reaction'); }); it('=> operateFileRecord: should return an array of type File', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateFileRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { post_id: 'ps81iqbddesfby8jayz7owg4yypoo', name: 'test_file', extension: '.jpg', size: 1000, create_at: 1609253011321, delete_at: 1609253011321, height: 20, update_at: 1609253011321, user_id: 'wqyby5r5pinxxdqhoaomtacdhc', }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('File'); }); it('=> operatePostMetadataRecord: should return an array of type PostMetadata', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operatePostMetadataRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { id: 'ps81i4yypoo', data: {}, postId: 'ps81iqbddesfby8jayz7owg4yypoo', type: 'opengraph', }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('PostMetadata'); }); it('=> operateDraftRecord: should return an array of type Draft', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateDraftRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { id: 'ps81i4yypoo', root_id: 'ps81iqbddesfby8jayz7owg4yypoo', message: 'draft message', channel_id: 'channel_idp23232e', files: [], }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('Draft'); }); it('=> operatePostsInChannelRecord: should return an array of type PostsInChannel', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operatePostsInChannelRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { id: 'ps81i4yypoo', channel_id: 'channel_idp23232e', earliest: 1608253011321, latest: 1609253011321, }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe( 'PostsInChannel', ); }); it('=> operateUserRecord: should return an array of type User', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateUserRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { id: '9ciscaqbrpd6d8s68k76xb9bte', is_bot: false, create_at: 1599457495881, update_at: 1607683720173, delete_at: 0, username: 'a.l', auth_service: 'saml', email: 'a.l@mattermost.com', email_verified: true, nickname: '', first_name: 'A', last_name: 'L', position: 'Mobile Engineer', roles: 'system_user', props: {}, notify_props: { desktop: 'all', desktop_sound: true, email: true, first_name: true, mention_keys: '', push: 'mention', channel: true, auto_responder_active: false, auto_responder_message: 'Hello, I am out of office and unable to respond to messages.', comments: 'never', desktop_notification_sound: 'Hello', push_status: 'online', }, last_password_update: 1604323112537, last_picture_update: 1604686302260, locale: 'en', timezone: { automaticTimezone: 'Indian/Mauritius', manualTimezone: '', useAutomaticTimezone: true, }, }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('User'); }); it('=> operatePreferenceRecord: should return an array of type Preference', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operatePreferenceRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: {user_id: '9ciscaqbrpd6d8s68k76xb9bte', category: 'tutorial_step', name: '9ciscaqbrpd6d8s68k76xb9bte', value: '2'}, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('Preference'); }); it('=> operateTeamMembershipRecord: should return an array of type TeamMembership', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateTeamMembershipRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { team_id: 'a', user_id: 'ab', roles: '3ngdqe1e7tfcbmam4qgnxp91bw', delete_at: 0, scheme_guest: false, scheme_user: true, scheme_admin: false, explicit_roles: '', }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('TeamMembership'); }); it('=> operateCustomEmojiRecord: should return an array of type CustomEmoji', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateCustomEmojiRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { id: 'i', create_at: 1580913641769, update_at: 1580913641769, delete_at: 0, creator_id: '4cprpki7ri81mbx8efixcsb8jo', name: 'boomI', }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('CustomEmoji'); }); it('=> operateGroupMembershipRecord: should return an array of type GroupMembership', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateGroupMembershipRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { user_id: 'u4cprpki7ri81mbx8efixcsb8jo', group_id: 'g4cprpki7ri81mbx8efixcsb8jo', }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('GroupMembership'); }); it('=> operateChannelMembershipRecord: should return an array of type ChannelMembership', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateChannelMembershipRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { channel_id: '17bfnb1uwb8epewp4q3x3rx9go', user_id: '9ciscaqbrpd6d8s68k76xb9bte', roles: 'wqyby5r5pinxxdqhoaomtacdhc', last_viewed_at: 1613667352029, msg_count: 3864, mention_count: 0, notify_props: { desktop: 'default', email: 'default', ignore_channel_mentions: 'default', mark_unread: 'mention', push: 'default', }, last_update_at: 1613667352029, scheme_guest: false, scheme_user: true, scheme_admin: false, explicit_roles: '', }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('ChannelMembership'); }); it('=> operateGroupRecord: should return an array of type Group', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateGroupRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { id: 'id_groupdfjdlfkjdkfdsf', name: 'mobile_team', display_name: 'mobile team', description: '', source: '', remote_id: '', create_at: 0, update_at: 0, delete_at: 0, has_syncables: true, }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('Group'); }); it('=> operateGroupsInTeamRecord: should return an array of type GroupsInTeam', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateGroupsInTeamRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { team_id: 'team_89', team_display_name: '', team_type: '', group_id: 'group_id89', auto_add: true, create_at: 0, delete_at: 0, update_at: 0, }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('GroupsInTeam'); }); it('=> operateGroupsInChannelRecord: should return an array of type GroupsInChannel', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateGroupsInChannelRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { auto_add: true, channel_display_name: '', channel_id: 'channelid', channel_type: '', create_at: 0, delete_at: 0, group_id: 'groupId', team_display_name: '', team_id: '', team_type: '', update_at: 0, }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('GroupsInChannel'); }); it('=> operateTeamRecord: should return an array of type Team', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateTeamRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { id: 'rcgiyftm7jyrxnmdfdfa1osd8zswby', create_at: 1445538153952, update_at: 1588876392150, delete_at: 0, display_name: 'Contributors', name: 'core', description: '', email: '', type: 'O', company_name: '', allowed_domains: '', invite_id: 'codoy5s743rq5mk18i7u5dfdfksz7e', allow_open_invite: true, last_team_icon_update: 1525181587639, scheme_id: 'hbwgrncq1pfcdkpotzidfdmarn95o', group_constrained: null, }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('Team'); }); it('=> operateTeamChannelHistoryRecord: should return an array of type Team', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateTeamChannelHistoryRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { team_id: 'a', channel_ids: ['ca', 'cb'], }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('TeamChannelHistory'); }); it('=> operateTeamSearchHistoryRecord: should return an array of type TeamSearchHistory', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateTeamSearchHistoryRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { team_id: 'a', term: 'termA', display_term: 'termA', created_at: 1445538153952, }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('TeamSearchHistory'); }); it('=> operateSlashCommandRecord: should return an array of type SlashCommand', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateSlashCommandRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { id: 'command_1', auto_complete: true, auto_complete_desc: 'mock_command', auto_complete_hint: 'hint', create_at: 1445538153952, creator_id: 'creator_id', delete_at: 1445538153952, description: 'description', display_name: 'display_name', icon_url: 'display_name', method: 'get', team_id: 'teamA', token: 'token', trigger: 'trigger', update_at: 1445538153953, url: 'url', username: 'userA', }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('SlashCommand'); }); it('=> operateMyTeamRecord: should return an array of type MyTeam', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateMyTeamRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { team_id: 'teamA', roles: 'roleA, roleB, roleC', is_unread: true, mentions_count: 3, }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('MyTeam'); }); it('=> operateChannelRecord: should return an array of type Channel', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateChannelRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { id: 'kow9j1ttnxwig7tnqgebg7dtipno', create_at: 1600185541285, update_at: 1604401077256, delete_at: 0, team_id: '', type: 'D', display_name: '', name: 'jui1zkzkhh357b4bejephjz5u8daw__9ciscaqbrpd6d8s68k76xb9bte', header: 'https://mattermost)', purpose: '', last_post_at: 1617311494451, total_msg_count: 585, extra_update_at: 0, creator_id: '', scheme_id: null, props: null, group_constrained: null, shared: null, }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('Channel'); }); it('=> operateMyChannelSettingsRecord: should return an array of type MyChannelSettings', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateMyChannelSettingsRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { channel_id: 'c', notify_props: { desktop: 'all', desktop_sound: true, email: true, first_name: true, mention_keys: '', push: 'mention', channel: true, }, }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('MyChannelSettings'); }); it('=> operateChannelInfoRecord: should return an array of type ChannelInfo', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateChannelInfoRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { channel_id: 'c', guest_count: 10, header: 'channel info header', member_count: 10, pinned_post_count: 3, purpose: 'sample channel ', }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('ChannelInfo'); }); it('=> operateMyChannelRecord: should return an array of type MyChannel', async () => { expect.assertions(3); const database = await createConnection(); expect(database).toBeTruthy(); const preparedRecords = await operateMyChannelRecord({ action: OperationType.CREATE, database: database!, value: { record: undefined, raw: { channel_id: 'cd', last_post_at: 1617311494451, last_viewed_at: 1617311494451, mentions_count: 3, message_count: 10, roles: 'guest', }, }, }); expect(preparedRecords).toBeTruthy(); expect(preparedRecords!.collection.modelClass.name).toBe('MyChannel'); }); });