Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Double Login Tests #6294

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions packages/realm-react/src/__tests__/UserProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2023 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
import React, { useEffect, useState } from "react";
import { AppProvider } from "../AppProvider";
import { renderHook, waitFor } from "@testing-library/react-native";

import { AppConfigBuilder } from "@realm/app-importer";
import { useEmailPasswordAuth } from "../useEmailPasswordAuth";
import { baseUrl, importApp } from "./helpers";
import { AuthOperationName } from "../types";
import { UserProvider, useUser } from "../UserProvider";
import { App } from "realm";

const testEmail = "[email protected]";
const testPassword = "password";

let userRegistered = false;
elle-j marked this conversation as resolved.
Show resolved Hide resolved

const Login = () => {
const { register, logIn, result } = useEmailPasswordAuth();
const [isUserRegistered, setIsUserRegistered] = useState(userRegistered);

useEffect(() => {
if (!isUserRegistered && !result.pending && result.operation !== AuthOperationName.Register) {
console.log("registering user");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log("registering user");

register({ email: testEmail, password: testPassword });
}
}, [register, isUserRegistered, result]);

useEffect(() => {
if (result.success && result.operation === AuthOperationName.Register) {
console.log("user registered");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log("user registered");

userRegistered = true;
setIsUserRegistered(true);
}
}, [result]);

useEffect(() => {
if (isUserRegistered && !result.pending && result.operation !== AuthOperationName.LogInWithEmailPassword) {
console.log("logging in user");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log("logging in user");

logIn({ email: testEmail, password: testPassword });
}
}, [logIn, isUserRegistered]);

return null;
};

function renderUserProvider(appId: string, baseUrl: string) {
const wrapper = ({ children }: { children: React.ReactNode }) => (
<AppProvider id={appId} baseUrl={baseUrl}>
<UserProvider fallback={<Login />}>{children}</UserProvider>
</AppProvider>
);
return renderHook(() => useUser(), { wrapper });
}

describe("UserProvider", () => {
describe("with auto confirm", () => {
let appId: string;
beforeAll(async () => {
const config = new AppConfigBuilder("test-app");
config.authProvider({
name: "local-userpass",
type: "local-userpass",
config: {
autoConfirm: true,
resetPasswordUrl: "http://localhost/resetPassword",
},
disabled: false,
});
({ appId } = await importApp(config.config));
});
it("it returns the current logged in user", async () => {
const { result } = renderUserProvider(appId, baseUrl);
await waitFor(() => expect(result.current).not.toBe(null));
});
it("it can login twice", async () => {
const { result } = renderUserProvider(appId, baseUrl);
await waitFor(() => expect(result.current).not.toBe(null));

const { refreshToken, id } = result.current;

const creds = Realm.Credentials.emailPassword({ email: testEmail, password: testPassword });
const realmApp = new App({ id: appId, baseUrl });

await realmApp.logIn(creds);

console.log("testing results: ", {
id,
currentId: realmApp?.currentUser?.id,
refreshToken,
currentRefreshToken: realmApp?.currentUser?.refreshToken,
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log("testing results: ", {
id,
currentId: realmApp?.currentUser?.id,
refreshToken,
currentRefreshToken: realmApp?.currentUser?.refreshToken,
});


expect(id).toEqual(realmApp?.currentUser?.id);
expect(refreshToken).not.toEqual(realmApp?.currentUser?.refreshToken);
});
});
});