add test for geoip

pull/10/head
undefined 4 years ago
parent 7651b0a697
commit a8bdfa8f30

@ -14,7 +14,7 @@ export interface Result {
display: string
}
function lookup(ip: string, locale: string) {
export function lookup(ip: string, locale: string): any {
const res: any = reader.get(ip);
if (!res) return {};
const ret: Result = { display: '' };
@ -25,11 +25,13 @@ function lookup(ip: string, locale: string) {
|| (res.country || res.registered_country).names.en;
}
if (res.city) ret.city = res.city.names[locale] || res.city.names.en;
ret.display = `${ret.continent} ${ret.country} ${ret.city}`;
ret.display = `${ret.continent} ${ret.country}${ret.city ? ` ${ret.city}` : ''}`;
return ret;
}
global.Hydro.lib.geoip = exports = {
provider: '<a href="http://www.maxmind.com" target="_blank">MaxMind</a>',
export const provider = '<a href="http://www.maxmind.com" target="_blank">MaxMind</a>';
global.Hydro.lib.geoip = {
provider,
lookup,
};

@ -1,6 +1,6 @@
{
"name": "@hydrooj/geoip",
"version": "1.1.7",
"version": "1.1.8",
"main": "package.json",
"repository": "git@github.com:hydro-dev/Hydro.git",
"author": "undefined <masnn0@outlook.com>",

@ -0,0 +1,9 @@
jest.mock('hydrooj/src/loader');
import * as lib from '@hydrooj/geoip/lib';
describe('geoip', () => {
test('MD5', () => {
expect(lib.lookup('127.0.0.1', 'zh_CN')).toStrictEqual({});
expect(lib.lookup('1.1.1.1', 'zh-CN').display).toStrictEqual('大洋洲 澳大利亚');
});
});

@ -55,7 +55,7 @@ export function edit(domainId: string, $set: any) {
return coll.updateOne({ _id: domainId }, { $set });
}
export async function inc(domainId: string, field: any, n: number): Promise<number | null> {
export async function inc(domainId: string, field: keyof DomainDoc, n: number): Promise<number | null> {
const res = await coll.findOneAndUpdate(
{ _id: domainId },
// FIXME

@ -1,5 +1,4 @@
import 'hydrooj/src/utils';
import 'hydrooj';
jest.mock('hydrooj/src/service/db');
export async function connect() {
@ -13,8 +12,10 @@ export async function connect() {
export async function dispose() {
const db = require('hydrooj/src/service/db');
await db.getClient().close();
await db.getClient2().close();
await db.getDb().close();
await db.getDb2().close();
await Promise.all([
db.getClient().close(),
db.getClient2().close(),
db.getDb().close(),
db.getDb2().close(),
]);
}

@ -1,8 +1,8 @@
import 'hydrooj';
import 'hydrooj/src/utils';
import md5 from 'hydrooj/src/lib/md5';
import sha1 from 'hydrooj/src/lib/sha1';
import pwhash from 'hydrooj/src/lib/hash.hydro';
import 'hydrooj/src/utils';
describe('crypto', () => {
const content1 = 'twd2akioi';

@ -16,6 +16,7 @@
"src"
],
"exclude": [
"__mocks__",
"bin",
"dist"
]

Loading…
Cancel
Save