test function need drone

adminDb
remrem 3 years ago
parent 648e2bbd24
commit 6937182f9b

@ -1,5 +1,6 @@
import 'package:shelf/shelf.dart'; import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart'; import 'package:shelf_router/shelf_router.dart';
import 'dart:convert';
// Class for all static function that handles api routes // Class for all static function that handles api routes
class API { class API {
@ -13,13 +14,16 @@ class API {
} }
// Check for authentication // Check for authentication
static Response authenticator(Request req) { static Future<Response> authenticator(Request req) async {
final mail = req.params['mail']; final List<String> required = ["mail", "password"];
final password = req.params['cyphered_password_hash'];
if (await checkRequiredFields(required, req)) {
return Response.ok('true'); return Response.ok('true');
} }
return Response.badRequest();
}
// Download sqlite password file // Download sqlite password file
static Response downloadPasswordDb(Request req) { static Response downloadPasswordDb(Request req) {
final mail = req.params['mail']; final mail = req.params['mail'];
@ -85,10 +89,11 @@ class API {
static Future<bool> checkRequiredFields( static Future<bool> checkRequiredFields(
List<String> fields, Request req) async { List<String> fields, Request req) async {
// json object read -> check dic keys // json object read -> check dic keys
final body = await req.readAsString(); var tmp = await req.readAsString();
final body = json.decode(tmp);
bool check = false; bool check = false;
for (String s in fields) { for (String s in fields) {
if (req.params['$s'] == "") { if (body['$s'] == "") {
return false; return false;
} }
} }

@ -23,17 +23,23 @@ void main() {
test('Root', () async { test('Root', () async {
final response = await get(Uri.parse('$host/')); final response = await get(Uri.parse('$host/'));
expect(response.statusCode, 200); expect(response.statusCode, 200);
expect(response.body, 'Hello, World!\n'); expect(response.body, 'Greetings from PassWorld!\n');
}); });
test('Echo', () async { // test('Echo', () async {
final response = await get(Uri.parse('$host/echo/hello')); // final response = await get(Uri.parse('$host/echo/hello'));
expect(response.statusCode, 200); // expect(response.statusCode, 200);
expect(response.body, 'hello\n'); // expect(response.body, 'hello\n');
}); // });
test('404', () async { test('404', () async {
final response = await get(Uri.parse('$host/foobar')); final response = await get(Uri.parse('$host/foobar'));
expect(response.statusCode, 404); expect(response.body, 'Route not found\n');
});
test('Auth', () async {
final resonse = await get(Uri.parse('$host/auth'));
expect(resonse.statusCode, 400);
expect(resonse.body, 'false');
}); });
} }

Loading…
Cancel
Save