import { ReceiptAliasController } from './receipt-alias.controller';

describe('ReceiptAlias controller security', () => {
  const receiptAliasServiceMock = {
    findAllForUser: jest.fn(),
    upsert: jest.fn(),
    update: jest.fn(),
    remove: jest.fn(),
  };

  const controller = new ReceiptAliasController(receiptAliasServiceMock as any);

  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('findAll scopear till @CurrentUser userId + role', () => {
    receiptAliasServiceMock.findAllForUser.mockResolvedValue([]);

    controller.findAll({ userId: 42, role: 'user' });

    expect(receiptAliasServiceMock.findAllForUser).toHaveBeenCalledWith(42, 'user');
  });

  it('upsert scopear till @CurrentUser userId + role', () => {
    const dto = { receiptName: 'Mjolk', productName: 'Mjolk' };
    receiptAliasServiceMock.upsert.mockResolvedValue({ id: 1 });

    controller.upsert(dto as any, { userId: 42, role: 'admin' });

    expect(receiptAliasServiceMock.upsert).toHaveBeenCalledWith(dto, 42, 'admin');
  });

  it('remove scopear till @CurrentUser userId + role', () => {
    receiptAliasServiceMock.remove.mockResolvedValue({ removed: true });

    controller.remove(10, { userId: 42, role: 'user' });

    expect(receiptAliasServiceMock.remove).toHaveBeenCalledWith(10, 42, 'user');
  });

  it('update scopear till @CurrentUser userId + role', () => {
    const dto = { receiptName: 'Arla mjolk 1l', productId: 7 };
    receiptAliasServiceMock.update.mockResolvedValue({ id: 10 });

    controller.update(10, dto as any, { userId: 42, role: 'user' });

    expect(receiptAliasServiceMock.update).toHaveBeenCalledWith(10, dto, 42, 'user');
  });

  it('update skickar med isGlobal i dto', () => {
    const dto = { receiptName: 'Arla mjolk 1l', productId: 7, isGlobal: true };
    receiptAliasServiceMock.update.mockResolvedValue({ id: 10 });

    controller.update(10, dto as any, { userId: 42, role: 'admin' });

    expect(receiptAliasServiceMock.update).toHaveBeenCalledWith(10, dto, 42, 'admin');
  });
});
