Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

1.9 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest';

const mocks = vi.hoisted(() => ({ apiFetch: vi.fn(), apiFetchJson: vi.fn() }));

vi.mock('@platform/transport/http', () => ({
  apiFetch: mocks.apiFetch,
  apiFetchJson: mocks.apiFetchJson,
  REFRESHED_TOKEN_HEADER: 'X-Refreshed-Token',
}));

import { createUser, deleteUser, updateUser } from './api';

describe('Identity user mutations', () => {
  beforeEach(() => {
    mocks.apiFetch.mockReset().mockImplementation(() => Promise.resolve(new Response('{}')));
    mocks.apiFetchJson.mockReset().mockResolvedValue({});
  });

  it('creates users with the backend DTO shape', async () => {
    const request = { display_name: 'Ada', email: '[email protected]', is_admin: true, password: 'secret' };

    await createUser(request);

    expect(mocks.apiFetchJson).toHaveBeenCalledWith('/api/v1/auth/users', {
      body: JSON.stringify(request),
      method: 'POST',
    });
  });

  it('encodes user ids for update and delete mutations', async () => {
    await updateUser('user/with space', { is_active: false });
    await deleteUser('user/with space');

    expect(mocks.apiFetch).toHaveBeenCalledWith('/api/v1/auth/users/user%2Fwith%20space', {
      body: JSON.stringify({ is_active: false }),
      headers: { 'Content-Type': 'application/json' },
      method: 'PATCH',
    });
    expect(mocks.apiFetch).toHaveBeenCalledWith('/api/v1/auth/users/user%2Fwith%20space', { method: 'DELETE' });
  });

  it("returns the edited user with the replacement token for the caller's own credential", async () => {
    mocks.apiFetch.mockResolvedValueOnce(
      new Response(JSON.stringify({ user_id: 'admin' }), { headers: { 'X-Refreshed-Token': 'replacement' } })
    );

    await expect(updateUser('admin', { password: 'new-password' })).resolves.toEqual({
      refreshedToken: 'replacement',
      user: { user_id: 'admin' },
    });
  });
});