Skip to content

Commit

Permalink
chre: Reorganize test code
Browse files Browse the repository at this point in the history
  • Loading branch information
manyuanrong committed Jan 13, 2021
1 parent 8298163 commit 4855e1b
Show file tree
Hide file tree
Showing 15 changed files with 699 additions and 659 deletions.
43 changes: 0 additions & 43 deletions tests/auth.ts

This file was deleted.

141 changes: 141 additions & 0 deletions tests/cases/00_uri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { parse } from "../../src/utils/uri.ts";
import { assertEquals } from "./../test.deps.ts";

export default function uriTests() {
Deno.test({
name: "should correctly parse mongodb://localhost",
fn() {
const options = parse("mongodb://localhost/");
assertEquals(options.db, "admin");
assertEquals(options.servers.length, 1);
assertEquals(options.servers[0].host, "localhost");
assertEquals(options.servers[0].port, 27017);
},
});

Deno.test({
name: "should correctly parse mongodb://localhost:27017",
fn() {
const options = parse("mongodb://localhost:27017/");
assertEquals(options.db, "admin");
assertEquals(options.servers.length, 1);
assertEquals(options.servers[0].host, "localhost");
assertEquals(options.servers[0].port, 27017);
},
});

Deno.test({
name:
"should correctly parse mongodb://localhost:27017/test?appname=hello%20world",
fn() {
const options = parse(
"mongodb://localhost:27017/test?appname=hello%20world",
);
assertEquals(options.appname, "hello world");
},
});

Deno.test({
name:
"should correctly parse mongodb://localhost/?safe=true&readPreference=secondary",
fn() {
const options = parse(
"mongodb://localhost/?safe=true&readPreference=secondary",
);
assertEquals(options.db, "admin");
assertEquals(options.servers.length, 1);
assertEquals(options.servers[0].host, "localhost");
assertEquals(options.servers[0].port, 27017);
},
});

Deno.test({
name: "should correctly parse mongodb://localhost:28101/",
fn() {
const options = parse("mongodb://localhost:28101/");
assertEquals(options.db, "admin");
assertEquals(options.servers.length, 1);
assertEquals(options.servers[0].host, "localhost");
assertEquals(options.servers[0].port, 28101);
},
});
Deno.test({
name: "should correctly parse mongodb://fred:foobar@localhost/baz",
fn() {
const options = parse("mongodb://fred:foobar@localhost/baz");
assertEquals(options.db, "baz");
assertEquals(options.servers.length, 1);
assertEquals(options.servers[0].host, "localhost");
assertEquals(options.credential!.username, "fred");
assertEquals(options.credential!.password, "foobar");
},
});

Deno.test({
name: "should correctly parse mongodb://fred:foo%20bar@localhost/baz",
fn() {
const options = parse("mongodb://fred:foo%20bar@localhost/baz");
assertEquals(options.db, "baz");
assertEquals(options.servers.length, 1);
assertEquals(options.servers[0].host, "localhost");
assertEquals(options.credential!.username, "fred");
assertEquals(options.credential!.password, "foo bar");
},
});

Deno.test({
name: "should correctly parse mongodb://%2Ftmp%2Fmongodb-27017.sock",
fn() {
const options = parse("mongodb://%2Ftmp%2Fmongodb-27017.sock");
assertEquals(options.servers.length, 1);
assertEquals(options.servers[0].domainSocket, "/tmp/mongodb-27017.sock");
assertEquals(options.db, "admin");
},
});

Deno.test({
name:
"should correctly parse mongodb://fred:foo@%2Ftmp%2Fmongodb-27017.sock",
fn() {
const options = parse("mongodb://fred:foo@%2Ftmp%2Fmongodb-27017.sock");
assertEquals(options.servers.length, 1);
assertEquals(options.servers[0].domainSocket, "/tmp/mongodb-27017.sock");
assertEquals(options.credential!.username, "fred");
assertEquals(options.credential!.password, "foo");
assertEquals(options.db, "admin");
},
});

Deno.test({
name:
"should correctly parse mongodb://fred:foo@%2Ftmp%2Fmongodb-27017.sock/somedb",
fn() {
const options = parse(
"mongodb://fred:foo@%2Ftmp%2Fmongodb-27017.sock/somedb",
);
assertEquals(options.servers.length, 1);
assertEquals(options.servers[0].domainSocket, "/tmp/mongodb-27017.sock");
assertEquals(options.credential!.username, "fred");
assertEquals(options.credential!.password, "foo");
assertEquals(options.db, "somedb");
},
});

Deno.test({
name:
"should correctly parse mongodb://fred:foo@%2Ftmp%2Fmongodb-27017.sock/somedb?safe=true",
fn() {
const options = parse(
"mongodb://fred:foo@%2Ftmp%2Fmongodb-27017.sock/somedb?safe=true",
);
assertEquals(options.servers.length, 1);
assertEquals(options.servers[0].domainSocket, "/tmp/mongodb-27017.sock");
assertEquals(options.credential!.username, "fred");
assertEquals(options.credential!.password, "foo");
assertEquals(options.db, "somedb");
assertEquals(options.safe, true);
},
});

// TODO: add more tests (https://github.com/mongodb/node-mongodb-native/blob/3.6/test/functional/url_parser.test.js)
}
162 changes: 162 additions & 0 deletions tests/cases/01_auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import {
cleanUsername,
clientFirstMessageBare,
HI,
passwordDigest,
} from "../../src/auth/mod.ts";
import { MongoClient } from "../../src/client.ts";
import { testWithClient } from "../common.ts";
import { assert, assertEquals } from "../test.deps.ts";

const hostname = "127.0.0.1";

interface PasswordValid {
username: string;
password: string;
digest: string;
}

const passwordValdis: PasswordValid[] = [
{
username: "user",
password: "pencil",
digest: "1c33006ec1ffd90f9cadcbcc0e118200",
},
{
username: "test",
password: "test",
digest: "a6de521abefc2fed4f5876855a3484f5",
},
];

export default function authTests() {
passwordValdis.forEach(({ username, password, digest }) => {
Deno.test({
name: `passwordDigest:${username}:${password}`,
fn() {
const digestRes: string = passwordDigest(username, password);
assertEquals(digestRes, digest);
},
});
});

Deno.test({
name: "clientFirstMessageBare",
fn() {
const username = "1234";
const nonce = new TextEncoder().encode("qwer");
const result: Uint8Array = clientFirstMessageBare(username, nonce);
const expected: Uint8Array = Uint8Array.from(
[
110,
61,
49,
50,
51,
52,
44,
114,
61,
99,
88,
100,
108,
99,
103,
61,
61,
],
);
assertEquals(expected, result);
},
});

Deno.test({
name: "cleanUsername",
fn() {
const username: string = "first=12,last=34";
const expected: string = "first=3D12=2Clast=34";
const result: string = cleanUsername(username);
assertEquals(expected, result);
},
});

const salt = "rQ9ZY3MntBeuP3E1TDVC4w";
const iter = 10000;
const data = "1c33006ec1ffd90f9cadcbcc0e118200";

Deno.test({
name: "HI",
fn() {
const saltedPassword = HI(
data,
(new TextEncoder()).encode(salt),
iter,
"sha1",
);
assertEquals(
saltedPassword,
[
72,
84,
156,
182,
17,
64,
30,
116,
86,
233,
7,
39,
65,
137,
142,
164,
0,
110,
78,
230,
],
);
},
});

testWithClient("createUser", async (client) => {
const db = client.database("test");
await db.createUser("user1", "y3mq3mpZ3J6PGfgg");
await db.createUser("user2", "Qa6WkQSuXF425sWZ");
});

Deno.test("connect authorization test 1 - test db", async () => {
var username = "user1";
var password = "y3mq3mpZ3J6PGfgg";
const client = new MongoClient();
await client.connect(
`mongodb://${username}:${password}@${hostname}:27017/test`,
);
const names = await client.listDatabases();
assert(names instanceof Array);
assert(names.length > 0);
client.close();
});

Deno.test("connect authorization test 2 - test db", async () => {
var username = "user2";
var password = "Qa6WkQSuXF425sWZ";
const client = new MongoClient();
await client.connect(
`mongodb://${username}:${password}@${hostname}:27017/test`,
);
const names = await client.listDatabases();
assert(names instanceof Array);
assert(names.length > 0);
client.close();
});

testWithClient("dropUser", async (client) => {
const db = client.database("test");
await db.dropUser("user1");
await db.dropUser("user2");
});
}
27 changes: 27 additions & 0 deletions tests/cases/02_connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { assert } from "../test.deps.ts";
import { MongoClient } from "../../src/client.ts";

const hostname = "127.0.0.1";

export default function connectTests() {
Deno.test("test connect", async () => {
const client = new MongoClient();
await client.connect(`mongodb://${hostname}:27017`);
const names = await client.listDatabases();
assert(names instanceof Array);
assert(names.length > 0);
client.close();
});

Deno.test("testconnect With Options", async () => {
const client = new MongoClient();
await client.connect({
servers: [{ host: hostname, port: 27017 }],
db: "admin",
});
const names = await client.listDatabases();
assert(names instanceof Array);
assert(names.length > 0);
client.close();
});
}
Loading

0 comments on commit 4855e1b

Please sign in to comment.