91 lines
2.8 KiB
TypeScript
91 lines
2.8 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
|
import { createAnonClient } from './setup.ts';
|
|
|
|
const client = createAnonClient();
|
|
const email = `test-${Date.now()}@example.com`;
|
|
const password = 'password123';
|
|
|
|
describe('Authentication', () => {
|
|
it('should sign up a new user', async () => {
|
|
const { data, error } = await client.auth.signUp({
|
|
email,
|
|
password,
|
|
});
|
|
|
|
expect(error).toBeNull();
|
|
expect(data.user).toBeDefined();
|
|
expect(data.user?.email).toBe(email);
|
|
expect(data.session).toBeDefined(); // Assuming auto-sign-in on signup
|
|
});
|
|
|
|
it('should sign in an existing user', async () => {
|
|
const { data, error } = await client.auth.signInWithPassword({
|
|
email,
|
|
password,
|
|
});
|
|
|
|
expect(error).toBeNull();
|
|
expect(data.session).toBeDefined();
|
|
expect(data.user).toBeDefined();
|
|
expect(data.user?.email).toBe(email);
|
|
});
|
|
|
|
it('should fail with incorrect password', async () => {
|
|
const { data, error } = await client.auth.signInWithPassword({
|
|
email,
|
|
password: 'wrongpassword',
|
|
});
|
|
|
|
expect(error).toBeDefined();
|
|
expect(data.session).toBeNull();
|
|
});
|
|
|
|
it('should persist session (getUser)', async () => {
|
|
// Ensure we are logged in
|
|
await client.auth.signInWithPassword({ email, password });
|
|
|
|
const { data, error } = await client.auth.getUser();
|
|
expect(error).toBeNull();
|
|
expect(data.user).toBeDefined();
|
|
expect(data.user?.email).toBe(email);
|
|
});
|
|
|
|
it('should refresh session', async () => {
|
|
// Ensure we are logged in
|
|
const { data: loginData } = await client.auth.signInWithPassword({ email, password });
|
|
expect(loginData.session).toBeDefined();
|
|
const oldAccessToken = loginData.session?.access_token;
|
|
const oldRefreshToken = loginData.session?.refresh_token;
|
|
|
|
// Refresh
|
|
const { data, error } = await client.auth.refreshSession();
|
|
expect(error).toBeNull();
|
|
expect(data.session).toBeDefined();
|
|
expect(data.session?.refresh_token).not.toBe(oldRefreshToken);
|
|
expect(data.user).toBeDefined();
|
|
});
|
|
|
|
it('should request password reset', async () => {
|
|
const { data, error } = await client.auth.resetPasswordForEmail(email);
|
|
expect(error).toBeNull();
|
|
expect(data).toBeDefined();
|
|
});
|
|
|
|
it('should update user metadata', async () => {
|
|
const { data: loginData } = await client.auth.signInWithPassword({ email, password });
|
|
expect(loginData.session).toBeDefined();
|
|
|
|
const { data, error } = await client.auth.updateUser({
|
|
data: { hello: 'world' },
|
|
});
|
|
|
|
expect(error).toBeNull();
|
|
expect(data.user).toBeDefined();
|
|
// Debug output
|
|
// console.log('Updated user:', JSON.stringify(data.user, null, 2));
|
|
// Check both potential locations
|
|
const metadata = data.user?.user_metadata || (data.user as any).raw_user_meta_data;
|
|
expect(metadata).toEqual({ hello: 'world' });
|
|
});
|
|
});
|