/** * Copyright 2022 Gravwell, Inc. All rights reserved. * * Contact: [legal@gravwell.io](mailto:legal@gravwell.io) * * This software may be modified and distributed under the terms of the MIT * license. See the LICENSE file for details. */ import { expectTypeOf } from 'expect-type'; import { AutoExtractorsFilter } from '~/functions/auto-extractors/download-many-auto-extractors'; import { IsValidAutoExtractorSyntaxResponse } from '~/functions/auto-extractors/is-valid-auto-extractor-syntax'; import { DashboardsFilter } from '~/functions/dashboards/get-many-dashboards'; import { MacrosFilter } from '~/functions/macros/get-many-macros'; import { MyNotificationsMessageReceived, MyNotificationsMessageSent, } from '~/functions/notifications/subscribe-to-my-notifications'; import { ScheduledQueriesFilter } from '~/functions/scheduled-tasks/get-many-scheduled-queries'; import { ScheduledScriptsFilter } from '~/functions/scheduled-tasks/get-many-scheduled-scripts'; import { SearchDownloadFormat } from '~/functions/searches/download-one-search'; import { GetAPIVersionResponse } from '~/functions/system/get-api-version'; import { SystemStatusCategory, SystemStatusMessageReceived, SystemStatusMessageSent, } from '~/functions/system/subscribe-to-many-system-informations'; import { APISubscription } from '~/functions/utils/api-subscription'; import { downloadFromURL, DownloadReturn } from '~/functions/utils/download-from-url'; import { File } from '~/functions/utils/file'; import { Actionable } from '~/models/actionable/actionable'; import { CreatableActionable } from '~/models/actionable/creatable-actionable'; import { UpdatableActionable } from '~/models/actionable/updatable-actionable'; import { AutoExtractor } from '~/models/auto-extractor/auto-extractor'; import { CreatableAutoExtractor } from '~/models/auto-extractor/creatable-auto-extractor'; import { GeneratedAutoExtractors } from '~/models/auto-extractor/generated-auto-extractors'; import { UpdatableAutoExtractor } from '~/models/auto-extractor/updatable-auto-extractor'; import { UploadableAutoExtractor } from '~/models/auto-extractor/uploadable-auto-extractor'; import { CreatableDashboard } from '~/models/dashboard/creatable-dashboard'; import { Dashboard } from '~/models/dashboard/dashboard'; import { UpdatableDashboard } from '~/models/dashboard/updatable-dashboard'; import { CreatableJSONEntry } from '~/models/entry/creatable-json-entry'; import { CreatableMultiLineEntry } from '~/models/entry/creatable-multi-line-entry'; import { CreatableFile } from '~/models/file/creatable-file'; import { FileMetadata } from '~/models/file/file-metadata'; import { UpdatableFile } from '~/models/file/updatable-file'; import { CreatableGroup } from '~/models/group/creatable-group'; import { Group } from '~/models/group/group'; import { UpdatableGroup } from '~/models/group/updatable-group'; import { BuildableKit } from '~/models/kit/buildable-kit'; import { InstallableKit } from '~/models/kit/installable-kit'; import { KitInstallationStatus } from '~/models/kit/kit-installation-status'; import { LocalKit } from '~/models/kit/local-kit'; import { RemoteKit } from '~/models/kit/remote-kit'; import { LogLevel } from '~/models/log-level/log-level'; import { CreatableMacro } from '~/models/macro/creatable-macro'; import { Macro } from '~/models/macro/macro'; import { UpdatableMacro } from '~/models/macro/updatable-macro'; import { CreatableBroadcastNotification } from '~/models/notification/creatable-broadcasted-notification'; import { CreatableTargetedNotification } from '~/models/notification/creatable-targeted-notification'; import { Notification } from '~/models/notification/notification'; import { TargetedNotificationTargetType } from '~/models/notification/targeted-notification'; import { UpdatableNotification } from '~/models/notification/updatable-notification'; import { CreatablePlaybook } from '~/models/playbook/creatable-playbook'; import { Playbook } from '~/models/playbook/playbook'; import { UpdatablePlaybook } from '~/models/playbook/updatable-playbook'; import { Query } from '~/models/query'; import { RenderModule } from '~/models/render-module/render-module'; import { CreatableResource } from '~/models/resource/creatable-resource'; import { Resource } from '~/models/resource/resource'; import { ResourceContentPreview } from '~/models/resource/resource-content-preview'; import { UpdatableResource } from '~/models/resource/updatable-resource'; import { CreatableSavedQuery } from '~/models/saved-query/creatable-saved-query'; import { SavedQuery } from '~/models/saved-query/saved-query'; import { UpdatableSavedQuery } from '~/models/saved-query/updatable-saved-query'; import { CreatableScheduledQuery } from '~/models/scheduled-task/creatable-scheduled-query'; import { CreatableScheduledScript } from '~/models/scheduled-task/creatable-scheduled-scripts'; import { ScheduledQuery } from '~/models/scheduled-task/scheduled-query'; import { ScheduledScript } from '~/models/scheduled-task/scheduled-script'; import { UpdatableScheduledQuery, UpdatableScheduledScript } from '~/models/scheduled-task/updatable-scheduled-task'; import { Script } from '~/models/script/script'; import { DataExplorerEntry } from '~/models/search/data-explorer-entry'; import { ElementFilter } from '~/models/search/element-filter'; import { ExplorerSearchSubscription } from '~/models/search/explorer-search-subscription'; import { Search } from '~/models/search/search'; import { SearchFilter } from '~/models/search/search-filter'; import { SearchSubscription } from '~/models/search/search-subscription'; import { Search2 } from '~/models/search/search2'; import { ValidatedQuery } from '~/models/search/validated-query'; import { SearchModule } from '~/models/search-module/search-module'; import { SystemSettings } from '~/models/system-settings/system-settings'; import { CreatableTemplate } from '~/models/template/creatable-template'; import { Template } from '~/models/template/template'; import { UpdatableTemplate } from '~/models/template/updatable-template'; import { CreatableToken } from '~/models/token/creatable-token'; import { Token } from '~/models/token/token'; import { TokenCapability } from '~/models/token/token-capability'; import { TokenWithSecret } from '~/models/token/token-with-secret'; import { UpdatableToken } from '~/models/token/updatable-token'; import { CreatableUser } from '~/models/user/creatable-user'; import { UpdatableUser } from '~/models/user/updatable-user'; import { User } from '~/models/user/user'; import { UserPreferences } from '~/models/user-preferences/user-preferences'; import { UserSessions } from '~/models/user-sessions/user-sessions'; import { unitTest } from '~/tests/test-types'; import { ID, NumericID, UUID } from '~/value-objects/id'; import { RawJSON } from '~/value-objects/json'; import { GravwellClient as Gravwell, GravwellClient } from './client'; import { GeneratableAutoExtractor } from './models/auto-extractor/generatable-auto-extractor'; describe('GravwellClient', () => { it( 'Should instantiate given a valid host', unitTest(() => { const fn = (): Gravwell => new GravwellClient('www.example.com'); expect(fn).not.toThrow(); }), ); it( 'Should update the functions host when the client host is updated', unitTest(() => { const client = new GravwellClient('www.example-a.com'); const fnA = client.tags.get.all; client.host = 'www.example-a.com'; expect(fnA === client.tags.get.all).toBeTrue(); client.host = 'www.example-b.com'; const fnB = client.tags.get.all; expect(fnA === client.tags.get.all).toBeFalse(); expect(fnB === client.tags.get.all).toBeTrue(); expect(fnA === fnB).toBeFalse(); }), ); xit( 'Should update the functions protocol when the client `.useEncryption` is updated', unitTest((): void => {}), ); it( 'Should have the correct function types', unitTest(() => { const client = new GravwellClient('www.example.com'); // Tags expectTypeOf(client.tags.get.all).toEqualTypeOf<() => Promise>>(); // System expectTypeOf(client.system.is.connected).toEqualTypeOf<() => Promise>(); expectTypeOf(client.system.get.settings).toEqualTypeOf<() => Promise>(); expectTypeOf(client.system.get.apiVersion).toEqualTypeOf<() => Promise>(); expectTypeOf(client.system.subscribeTo.information).toEqualTypeOf< ( statusCategories: Array, ) => Promise> >(); // Users expectTypeOf(client.users.get.me).toEqualTypeOf<() => Promise>(); expectTypeOf(client.users.get.one).toEqualTypeOf<(userID: NumericID) => Promise>(); expectTypeOf(client.users.get.many).toEqualTypeOf<(filter?: { groupID?: NumericID }) => Promise>>(); expectTypeOf(client.users.get.all).toEqualTypeOf<() => Promise>>(); expectTypeOf(client.users.create.one).toEqualTypeOf<(data: CreatableUser) => Promise>(); expectTypeOf(client.users.update.me).toEqualTypeOf<(data: Omit) => Promise>(); expectTypeOf(client.users.update.one).toEqualTypeOf<(data: UpdatableUser) => Promise>(); expectTypeOf(client.users.delete.one).toEqualTypeOf<(userID: NumericID) => Promise>(); // User preferences expectTypeOf(client.userPreferences.get.one).toEqualTypeOf<(userID: string) => Promise>(); expectTypeOf(client.userPreferences.get.all).toEqualTypeOf<() => Promise>>(); expectTypeOf(client.userPreferences.update.one).toEqualTypeOf< (userID: string, preferences: UserPreferences) => Promise >(); expectTypeOf(client.userPreferences.delete.one).toEqualTypeOf<(userID: string) => Promise>(); // Auth expectTypeOf(client.auth.login.one).toEqualTypeOf<(username: string, password: string) => Promise>(); expectTypeOf(client.auth.logout.one).toEqualTypeOf<(userAuthToken: string) => Promise>(); expectTypeOf(client.auth.logout.all).toEqualTypeOf<() => Promise>(); expectTypeOf(client.auth.get.many.activeSessions).toEqualTypeOf< (filter: { userID: string }) => Promise >(); // Notifications expectTypeOf(client.notifications.create.one.broadcasted).toEqualTypeOf< (creatable: CreatableBroadcastNotification) => Promise >(); expectTypeOf(client.notifications.create.one.targeted).toEqualTypeOf< ( targetType: TargetedNotificationTargetType, creatable: Omit, ) => Promise >(); expectTypeOf(client.notifications.get.mine).toEqualTypeOf<() => Promise>>(); expectTypeOf(client.notifications.subscribeTo.mine).toEqualTypeOf< (options?: { pollInterval?: number | undefined; }) => Promise> >(); expectTypeOf(client.notifications.update.one).toEqualTypeOf< (updatable: UpdatableNotification) => Promise >(); expectTypeOf(client.notifications.delete.one).toEqualTypeOf<(notificationID: string) => Promise>(); // Web server expectTypeOf(client.webServer.restart).toEqualTypeOf<() => Promise>(); expectTypeOf(client.webServer.is.distributed).toEqualTypeOf<() => Promise>(); // Indexers expectTypeOf(client.indexers.restart).toEqualTypeOf<() => Promise>(); // Ingestors expectTypeOf(client.entries.ingest.one.json).toEqualTypeOf<(entry: CreatableJSONEntry) => Promise>(); expectTypeOf(client.entries.ingest.many.json).toEqualTypeOf< (entries: Array) => Promise >(); expectTypeOf(client.entries.ingest.byLine).toEqualTypeOf<(entry: CreatableMultiLineEntry) => Promise>(); // Logs expectTypeOf(client.logs.get.levels).toEqualTypeOf< () => Promise<{ current: LogLevel | 'off'; available: Array }> >(); expectTypeOf(client.logs.set.level).toEqualTypeOf<(level: LogLevel | 'off') => Promise>(); expectTypeOf(client.logs.create.one).toEqualTypeOf<(level: LogLevel, message: string) => Promise>(); // Search status expectTypeOf(client.searchStatus.get.authorizedTo.me).toEqualTypeOf<() => Promise>>(); expectTypeOf(client.searchStatus.get.one.status).toEqualTypeOf<(searchID: NumericID) => Promise>(); expectTypeOf(client.searchStatus.get.all).toEqualTypeOf<() => Promise>>(); // Search history expectTypeOf(client.searchHistory.get.authorizedTo.user).toEqualTypeOf< (userID: string) => Promise> >(); expectTypeOf(client.searchHistory.get.many).toEqualTypeOf< (filter?: DashboardsFilter) => Promise> >(); expectTypeOf(client.searchHistory.get.mine).toEqualTypeOf<() => Promise>>(); expectTypeOf(client.searchHistory.get.all).toEqualTypeOf<() => Promise>>(); // Searches expectTypeOf(client.searches.background.one).toEqualTypeOf<(searchID: NumericID) => Promise>(); expectTypeOf(client.searches.save.one).toEqualTypeOf<(searchID: NumericID) => Promise>(); expectTypeOf(client.searches.delete.one).toEqualTypeOf<(searchID: NumericID) => Promise>(); expectTypeOf(client.searches.download.one).toEqualTypeOf< (searchID: ID, downloadFormat: SearchDownloadFormat) => ReturnType >(); expectTypeOf(client.searches.create.one).toEqualTypeOf< ( query: Query, options?: { filter?: Partial | undefined; metadata?: RawJSON | undefined; noHistory?: boolean }, ) => Promise >(); expectTypeOf(client.searches.stop.one).toEqualTypeOf<(searchID: string) => Promise>(); // Search modules expectTypeOf(client.searchModules.get.all).toEqualTypeOf<() => Promise>>(); // Render modules expectTypeOf(client.renderModules.get.all).toEqualTypeOf<() => Promise>>(); // Scripts expectTypeOf(client.scriptLibraries.get.one).toEqualTypeOf< ( scriptPath: string, options?: { repository?: string | undefined; commitID?: string | undefined }, ) => Promise