vorteil/direktiv

View on GitHub
ui/e2e/gateway/consumers/utils.ts

Summary

Maintainability
B
4 hrs
Test Coverage
import { ConsumerSchemaType } from "~/api/gateway/schema";
import { getConsumers } from "~/api/gateway/query/getConsumers";
import { headers } from "e2e/utils/testutils";

type CreateRedisConsumerFileParams = {
  username?: string;
  password?: string;
};

export const createRedisConsumerFile = ({
  username = "userA",
  password = "password",
}: CreateRedisConsumerFileParams = {}) => `direktiv_api: consumer/v1
username: ${username}
password: ${password}
api_key: "123456789"
groups: 
- "group1"
- "group2"
tags:
  - "tag1"
`;

type FindConsumerWithApiRequestParams = {
  namespace: string;
  match: (consumer: ConsumerSchemaType) => boolean;
};

// type ErrorType = { response: { status?: number } };

export const findConsumerWithApiRequest = async ({
  namespace,
  match,
}: FindConsumerWithApiRequestParams) => {
  try {
    const { data: consumers } = await getConsumers({
      urlParams: {
        baseUrl: process.env.PLAYWRIGHT_UI_BASE_URL,
        namespace,
      },
      headers,
    });
    return consumers.find(match);
  } catch (error) {
    // Temporary workaround: Until DIR-1503 is resolved, fail silently even on
    // 500 errors. Ideally, we should only catch 404s and still throw unexpected errors
    // (as implemented in the commented code).
    return false;
    // const typedError = error as ErrorType;
    // if (typedError.response.status === 404) {
    //   // fail silently to allow for using poll() in tests
    //   return false;
    // }
    // throw new Error(
    //   `Unexpected error ${typedError?.response?.status} during lookup of consumer ${match} in namespace ${namespace}`
    // );
  }
};