From 9a7a7d29e1ad9abd93b0fa0100e8391d1ae0617a Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sat, 10 Dec 2016 13:51:02 -0500 Subject: [PATCH] Custom plug-ins :) --- .../Start_Server__PRODUCTION_.xml | 9 +++++ .idea/runConfigurations/Users_Tests.xml | 6 ++++ analysis_options.yaml | 0 bin/server.dart | 1 - lib/angel.dart | 1 + lib/src/config/config.dart | 2 ++ lib/src/config/plugins/plugins.dart | 9 +++++ lib/src/models/user.dart | 18 +++++----- lib/src/routes/controllers/auth.dart | 12 +++---- lib/src/routes/routes.dart | 2 +- lib/src/services/services.dart | 2 +- lib/src/services/user.dart | 18 +++++----- pubspec.yaml | 1 + test/services/users.dart | 34 ------------------- test/services/users_test.dart | 27 +++++++++++++++ 15 files changed, 81 insertions(+), 61 deletions(-) create mode 100644 .idea/runConfigurations/Start_Server__PRODUCTION_.xml create mode 100644 .idea/runConfigurations/Users_Tests.xml create mode 100644 analysis_options.yaml create mode 100644 lib/src/config/plugins/plugins.dart delete mode 100644 test/services/users.dart create mode 100644 test/services/users_test.dart diff --git a/.idea/runConfigurations/Start_Server__PRODUCTION_.xml b/.idea/runConfigurations/Start_Server__PRODUCTION_.xml new file mode 100644 index 0000000..f10a556 --- /dev/null +++ b/.idea/runConfigurations/Start_Server__PRODUCTION_.xml @@ -0,0 +1,9 @@ + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Users_Tests.xml b/.idea/runConfigurations/Users_Tests.xml new file mode 100644 index 0000000..a5f788b --- /dev/null +++ b/.idea/runConfigurations/Users_Tests.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..e69de29 diff --git a/bin/server.dart b/bin/server.dart index cf61b66..48d5fdb 100755 --- a/bin/server.dart +++ b/bin/server.dart @@ -4,7 +4,6 @@ import 'dart:async'; import 'dart:io'; import 'package:angel/angel.dart'; import 'package:angel_diagnostics/angel_diagnostics.dart'; -import 'package:angel_framework/angel_framework.dart'; import 'package:intl/intl.dart'; main() async { diff --git a/lib/angel.dart b/lib/angel.dart index 17bfe66..d8a008e 100644 --- a/lib/angel.dart +++ b/lib/angel.dart @@ -6,6 +6,7 @@ import 'package:angel_framework/angel_framework.dart'; import 'src/config/config.dart' as configuration; import 'src/routes/routes.dart' as routes; import 'src/services/services.dart' as services; +export 'src/services/services.dart'; /// Creates and configures the server instance. Future createServer() async { diff --git a/lib/src/config/config.dart b/lib/src/config/config.dart index 7dde821..32df5a6 100644 --- a/lib/src/config/config.dart +++ b/lib/src/config/config.dart @@ -5,9 +5,11 @@ import 'dart:io'; import 'package:angel_configuration/angel_configuration.dart'; import 'package:angel_framework/angel_framework.dart'; import 'package:angel_mustache/angel_mustache.dart'; +import 'plugins/plugins.dart' as plugins; /// This is a perfect place to include configuration and load plug-ins. configureServer(Angel app) async { await app.configure(loadConfigurationFile()); await app.configure(mustache(new Directory('views'))); + await plugins.configureServer(app); } diff --git a/lib/src/config/plugins/plugins.dart b/lib/src/config/plugins/plugins.dart new file mode 100644 index 0000000..e7b4162 --- /dev/null +++ b/lib/src/config/plugins/plugins.dart @@ -0,0 +1,9 @@ +/// Custom plugins go here. +library angel.config.plugins; + +import 'dart:async'; +import 'package:angel_framework/angel_framework.dart'; + +Future configureServer(Angel app) async { + // Include any plugins you have made here. +} \ No newline at end of file diff --git a/lib/src/models/user.dart b/lib/src/models/user.dart index 8ea1144..8478f23 100644 --- a/lib/src/models/user.dart +++ b/lib/src/models/user.dart @@ -26,18 +26,18 @@ class User extends Model { factory User.fromMap(Map data) => new User( id: data['id'], - email: data["email"], - username: data["username"], - password: data["password"], - roles: data["roles"]); + email: data['email'], + username: data['username'], + password: data['password'], + roles: data['roles']); Map toJson() { return { - "id": id, - "email": email, - "username": username, - "password": password, - "roles": roles + 'id': id, + 'email': email, + 'username': username, + 'password': password, + 'roles': roles }; } } diff --git a/lib/src/routes/controllers/auth.dart b/lib/src/routes/controllers/auth.dart index d28d7fa..b5c37d4 100644 --- a/lib/src/routes/controllers/auth.dart +++ b/lib/src/routes/controllers/auth.dart @@ -4,17 +4,17 @@ import 'package:angel_auth/angel_auth.dart'; import 'package:angel_framework/angel_framework.dart'; import '../../services/user.dart'; -@Expose("/api/auth") +@Expose('/api/auth') class AuthController extends Controller { final AngelAuth auth = new AngelAuth(); - deserializer(String id) async => app.service("api/users").read(id); + deserializer(String id) async => app.service('api/users').read(id); serializer(User user) async => user.id; /// Attempt to log a user in verifier(UserService Users) { return (String username, String password) async { - List users = await Users.index({"username": username}); + List users = await Users.index({'username': username}); if (users.isNotEmpty) { var hash = hashPassword(password); @@ -36,14 +36,14 @@ class AuthController extends Controller { await app.configure(auth); } - bool loggedIn(RequestContext req) => req.session["userId"] != null; + bool loggedIn(RequestContext req) => req.session['userId'] != null; - @Expose("/login", method: "POST") + @Expose('/login', method: 'POST') login(RequestContext req) async { // Include log-in logic here... } - @Expose("/register", method: "POST") + @Expose('/register', method: 'POST') register(RequestContext req, UserService Users) async { // And your registration logic... } diff --git a/lib/src/routes/routes.dart b/lib/src/routes/routes.dart index 89eabd9..0f55859 100644 --- a/lib/src/routes/routes.dart +++ b/lib/src/routes/routes.dart @@ -26,7 +26,7 @@ configureAfter(Angel app) async { // Default error handler app.onError( - (e, req, res) async => res.render("error", {"message": e.message})); + (e, req, res) async => res.render('error', {'message': e.message})); } configureServer(Angel app) async { diff --git a/lib/src/services/services.dart b/lib/src/services/services.dart index 8cc3c9b..8560314 100644 --- a/lib/src/services/services.dart +++ b/lib/src/services/services.dart @@ -7,7 +7,7 @@ import 'package:mongo_dart/mongo_dart.dart'; import 'user.dart' as User; configureServer(Angel app) async { - Db db = new Db(app.properties["mongo_db"]); + Db db = new Db(app.properties['mongo_db']); await db.open(); await app.configure(User.configureServer(db)); diff --git a/lib/src/services/user.dart b/lib/src/services/user.dart index d59199d..2585836 100644 --- a/lib/src/services/user.dart +++ b/lib/src/services/user.dart @@ -8,9 +8,9 @@ export '../models/user.dart'; configureServer(Db db) { return (Angel app) async { - app.use("/api/users", new UserService(db.collection("users"))); + app.use('/api/users', new UserService(db.collection('users'))); - HookedService service = app.service("api/users"); + HookedService service = app.service('api/users'); app.container.singleton(service.inner); }; } @@ -32,7 +32,7 @@ class UserService extends Service { @override index([Map params]) { - if (params != null && params.containsKey("provider")) { + if (params != null && params.containsKey('provider')) { // Nobody needs to see the entire user list except for the server. throw new AngelHttpException.Forbidden(); } @@ -42,19 +42,19 @@ class UserService extends Service { @override create(data, [Map params]) { - if (params != null && params.containsKey("provider")) { + if (params != null && params.containsKey('provider')) { // Deny creating users to the public - this should be done by the server only. throw new AngelHttpException.Forbidden(); } try { - Validate.isKeyInMap("username", data); - Validate.isKeyInMap("password", data); - Validate.isEmail(data["email"]); - data["password"] = hashPassword(data["password"]); + Validate.isKeyInMap('username', data); + Validate.isKeyInMap('password', data); + Validate.isEmail(data['email']); + data['password'] = hashPassword(data['password']); } catch (e) { throw new AngelHttpException.BadRequest( - message: "User must have a username, e-mail address and password."); + message: 'User must have a username, e-mail address and password.'); } return _inner.create(data, params); diff --git a/pubspec.yaml b/pubspec.yaml index 5f559df..bfe71f3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,6 +14,7 @@ dependencies: angel_mustache: ^1.0.0-dev angel_proxy: ^1.0.0-dev angel_static: ^1.1.0-dev + angel_test: ^1.0.0-dev mailer: ^1.1.0+4 validate: ^1.5.2 dev_dependencies: diff --git a/test/services/users.dart b/test/services/users.dart deleted file mode 100644 index c09950f..0000000 --- a/test/services/users.dart +++ /dev/null @@ -1,34 +0,0 @@ -import 'dart:io'; -import 'package:angel/angel.dart'; -import 'package:angel_framework/angel_framework.dart'; -import 'package:http/http.dart'; -import 'package:test/test.dart'; - -main() async { - group('services.users', () { - Angel app; - Client client = new Client(); - Map headers = { - HttpHeaders.ACCEPT: ContentType.JSON.mimeType, - HttpHeaders.CONTENT_TYPE: ContentType.JSON.mimeType - }; - HttpServer server; - String url; - - setUp(() async { - Angel app = await createServer(); - server = await app.startServer(InternetAddress.LOOPBACK_IP_V4, 3000); - url = "http://localhost:3000"; - }); - - tearDown(() async { - await server.close(force: true); - client.close(); - }); - - test('index users', () async { - Response response = await client.get("$url/api/users"); - expect(response.statusCode, equals(HttpStatus.OK)); - }); - }); -} diff --git a/test/services/users_test.dart b/test/services/users_test.dart new file mode 100644 index 0000000..8cf7cdf --- /dev/null +++ b/test/services/users_test.dart @@ -0,0 +1,27 @@ +import 'dart:io'; +import 'package:angel/angel.dart'; +import 'package:angel_framework/angel_framework.dart'; +import 'package:angel_test/angel_test.dart'; +import 'package:test/test.dart'; + +main() async { + Angel app; + TestClient client; + + setUp(() async { + app = await createServer(); + client = await connectTo(app, saveSession: false); + }); + + tearDown(() async { + await client.close(); + app = null; + }); + + test('index users', () async { + final response = await client.get('/api/users'); + + // By default, we locked this away from the Internet... + expect(response, hasStatus(HttpStatus.FORBIDDEN)); + }); +}