Fix tests.
This commit is contained in:
parent
10f3816810
commit
adad73a9e2
|
@ -197,7 +197,10 @@ export class FlagSet<V extends Flag<any>, T extends { [key: string]: V }> {
|
||||||
|
|
||||||
let userId = identity?._id
|
let userId = identity?._id
|
||||||
if (!userId) {
|
if (!userId) {
|
||||||
userId = context.getIP()
|
const ip = context.getIP()
|
||||||
|
if (ip) {
|
||||||
|
userId = crypto.createHash("sha512").update(ip).digest("hex")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let tenantId = identity?.tenantId
|
let tenantId = identity?.tenantId
|
||||||
|
|
|
@ -18,7 +18,6 @@ interface TestCase {
|
||||||
identity?: Partial<IdentityContext>
|
identity?: Partial<IdentityContext>
|
||||||
environmentFlags?: string
|
environmentFlags?: string
|
||||||
posthogFlags?: PostHogFlags
|
posthogFlags?: PostHogFlags
|
||||||
licenseFlags?: Array<string>
|
|
||||||
expected?: Partial<FlagValues<typeof schema>>
|
expected?: Partial<FlagValues<typeof schema>>
|
||||||
errorMessage?: string | RegExp
|
errorMessage?: string | RegExp
|
||||||
}
|
}
|
||||||
|
@ -117,17 +116,6 @@ describe("feature flags", () => {
|
||||||
},
|
},
|
||||||
expected: { TEST_BOOLEAN: true },
|
expected: { TEST_BOOLEAN: true },
|
||||||
},
|
},
|
||||||
{
|
|
||||||
it: "should be able to set boolean flags through the license",
|
|
||||||
licenseFlags: ["TEST_BOOLEAN"],
|
|
||||||
expected: { TEST_BOOLEAN: true },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
it: "should not be able to override a negative environment flag from license",
|
|
||||||
environmentFlags: "default:!TEST_BOOLEAN",
|
|
||||||
licenseFlags: ["TEST_BOOLEAN"],
|
|
||||||
expected: { TEST_BOOLEAN: false },
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
it: "should not error on unrecognised PostHog flag",
|
it: "should not error on unrecognised PostHog flag",
|
||||||
posthogFlags: {
|
posthogFlags: {
|
||||||
|
@ -135,18 +123,12 @@ describe("feature flags", () => {
|
||||||
},
|
},
|
||||||
expected: flags.defaults(),
|
expected: flags.defaults(),
|
||||||
},
|
},
|
||||||
{
|
|
||||||
it: "should not error on unrecognised license flag",
|
|
||||||
licenseFlags: ["UNDEFINED"],
|
|
||||||
expected: flags.defaults(),
|
|
||||||
},
|
|
||||||
])(
|
])(
|
||||||
"$it",
|
"$it",
|
||||||
async ({
|
async ({
|
||||||
identity,
|
identity,
|
||||||
environmentFlags,
|
environmentFlags,
|
||||||
posthogFlags,
|
posthogFlags,
|
||||||
licenseFlags,
|
|
||||||
expected,
|
expected,
|
||||||
errorMessage,
|
errorMessage,
|
||||||
}) => {
|
}) => {
|
||||||
|
@ -162,8 +144,6 @@ describe("feature flags", () => {
|
||||||
env.POSTHOG_API_HOST = "https://us.i.posthog.com"
|
env.POSTHOG_API_HOST = "https://us.i.posthog.com"
|
||||||
}
|
}
|
||||||
|
|
||||||
const ctx = { user: { license: { features: licenseFlags || [] } } }
|
|
||||||
|
|
||||||
await withEnv(env, async () => {
|
await withEnv(env, async () => {
|
||||||
// We need to pass in node-fetch here otherwise nock won't get used
|
// We need to pass in node-fetch here otherwise nock won't get used
|
||||||
// because posthog-node uses axios under the hood.
|
// because posthog-node uses axios under the hood.
|
||||||
|
@ -185,18 +165,13 @@ describe("feature flags", () => {
|
||||||
|
|
||||||
await context.doInIdentityContext(fullIdentity, async () => {
|
await context.doInIdentityContext(fullIdentity, async () => {
|
||||||
if (errorMessage) {
|
if (errorMessage) {
|
||||||
await expect(flags.fetch(ctx as UserCtx)).rejects.toThrow(
|
await expect(flags.fetch()).rejects.toThrow(errorMessage)
|
||||||
errorMessage
|
|
||||||
)
|
|
||||||
} else if (expected) {
|
} else if (expected) {
|
||||||
const values = await flags.fetch(ctx as UserCtx)
|
const values = await flags.fetch()
|
||||||
expect(values).toMatchObject(expected)
|
expect(values).toMatchObject(expected)
|
||||||
|
|
||||||
for (const [key, expectedValue] of Object.entries(expected)) {
|
for (const [key, expectedValue] of Object.entries(expected)) {
|
||||||
const value = await flags.get(
|
const value = await flags.get(key as keyof typeof schema)
|
||||||
key as keyof typeof schema,
|
|
||||||
ctx as UserCtx
|
|
||||||
)
|
|
||||||
expect(value).toBe(expectedValue)
|
expect(value).toBe(expectedValue)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -252,8 +227,8 @@ describe("feature flags", () => {
|
||||||
POSTHOG_TOKEN: "test",
|
POSTHOG_TOKEN: "test",
|
||||||
}
|
}
|
||||||
|
|
||||||
const ctx = { ip: "127.0.0.1" } as UserCtx
|
const ip = "127.0.0.1"
|
||||||
const hashedIp = crypto.createHash("sha512").update(ctx.ip).digest("hex")
|
const hashedIp = crypto.createHash("sha512").update(ip).digest("hex")
|
||||||
|
|
||||||
await withEnv(env, async () => {
|
await withEnv(env, async () => {
|
||||||
mockPosthogFlags(
|
mockPosthogFlags(
|
||||||
|
@ -273,9 +248,11 @@ describe("feature flags", () => {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
await context.doInTenant("default", async () => {
|
await context.doInIPContext(ip, async () => {
|
||||||
const result = await flags.fetch(ctx)
|
await context.doInTenant("default", async () => {
|
||||||
expect(result.TEST_BOOLEAN).toBe(true)
|
const result = await flags.fetch()
|
||||||
|
expect(result.TEST_BOOLEAN).toBe(true)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
shutdown()
|
shutdown()
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { doInIPContext } from "../context"
|
||||||
|
|
||||||
export default async (ctx: Ctx, next: any) => {
|
export default async (ctx: Ctx, next: any) => {
|
||||||
if (ctx.ip) {
|
if (ctx.ip) {
|
||||||
doInIPContext(ctx.ip, () => {
|
return await doInIPContext(ctx.ip, () => {
|
||||||
return next()
|
return next()
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue