From 6e6eb65a8de12baaccdbda8c0b14e9fd4587bd47 Mon Sep 17 00:00:00 2001 From: necessarylion Date: Wed, 29 Nov 2023 18:06:12 +0630 Subject: [PATCH 1/5] add test for websocket --- packages/dox-websocket/pubspec.yaml | 1 + .../dox-websocket/test/config/app_config.dart | 52 +++++++++++ .../dox-websocket/test/config/router.dart | 23 +++++ .../test/dox_websocket_test.dart | 92 +++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 packages/dox-websocket/test/config/app_config.dart create mode 100644 packages/dox-websocket/test/config/router.dart create mode 100644 packages/dox-websocket/test/dox_websocket_test.dart diff --git a/packages/dox-websocket/pubspec.yaml b/packages/dox-websocket/pubspec.yaml index 3a54ec1..2a536bb 100644 --- a/packages/dox-websocket/pubspec.yaml +++ b/packages/dox-websocket/pubspec.yaml @@ -13,3 +13,4 @@ dependencies: dev_dependencies: lints: ^2.0.0 test: ^1.21.0 + dox_core: ^2.0.0-alpha.2 diff --git a/packages/dox-websocket/test/config/app_config.dart b/packages/dox-websocket/test/config/app_config.dart new file mode 100644 index 0000000..b9edbd1 --- /dev/null +++ b/packages/dox-websocket/test/config/app_config.dart @@ -0,0 +1,52 @@ +import 'package:dox_core/cache/drivers/file/file_cache_driver.dart'; +import 'package:dox_core/dox_core.dart'; +import 'package:dox_core/utils/logger.dart'; + +import 'router.dart'; + +class ResponseHandler extends ResponseHandlerInterface { + @override + void handle(DoxResponse res) {} +} + +class Config extends AppConfig { + @override + int get totalIsolate => 3; + + @override + String get appKey => '4HyiSrq4N5Nfg6bOadIhbFEI8zbUkpxt'; + + int _serverPort = 3001; + + @override + int get serverPort => _serverPort; + + set serverPort(int val) => _serverPort = val; + + @override + CORSConfig get cors => CORSConfig( + allowOrigin: '*', + allowMethods: '*', + allowCredentials: true, + ); + + @override + List get routers => [WebsocketRouter()]; + + @override + void Function(Object? error, StackTrace stackTrace) get errorHandler => + (Object? error, StackTrace stackTrace) { + DoxLogger.danger(error); + }; + + @override + CacheConfig get cacheConfig => CacheConfig( + defaultDriver: 'redis', + drivers: { + 'file': FileCacheDriver(), + }, + ); + + @override + ResponseHandlerInterface get responseHandler => ResponseHandler(); +} diff --git a/packages/dox-websocket/test/config/router.dart b/packages/dox-websocket/test/config/router.dart new file mode 100644 index 0000000..eda15da --- /dev/null +++ b/packages/dox-websocket/test/config/router.dart @@ -0,0 +1,23 @@ +import 'package:dox_core/dox_core.dart'; +import 'package:dox_websocket/dox_websocket.dart'; + +class WebsocketRouter implements Router { + @override + List get middleware => []; + + @override + String get prefix => ''; + + @override + void register() { + Route.websocket('ws', (WebsocketEvent event) { + event.on('intro', (WebsocketEmitter emitter, dynamic message) { + emitter.room('ws').emit('intro', message); + }); + + event.on('json', (WebsocketEmitter emitter, dynamic message) { + emitter.emitExceptSender('json', message); + }); + }); + } +} diff --git a/packages/dox-websocket/test/dox_websocket_test.dart b/packages/dox-websocket/test/dox_websocket_test.dart new file mode 100644 index 0000000..bd4c32a --- /dev/null +++ b/packages/dox-websocket/test/dox_websocket_test.dart @@ -0,0 +1,92 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:dox_core/dox_core.dart'; +import 'package:dox_core/utils/json.dart'; +import 'package:dox_websocket/dox_websocket.dart'; +import 'package:ioredis/ioredis.dart'; +import 'package:test/test.dart'; + +import 'config/app_config.dart'; + +Config config = Config(); +String baseUrl = 'http://localhost:${config.serverPort}'; + +class WebsocketService implements DoxService { + @override + void setup() { + Redis sub = Redis(); + Redis pub = sub.duplicate(); + + WebsocketServer io = WebsocketServer(Dox()); + io.adapter(WebsocketRedisAdapter( + subscriber: sub, + publisher: pub, + )); + } +} + +void main() { + group('Websocket |', () { + setUpAll(() async { + Dox().initialize(config); + Dox().addService(WebsocketService()); + await Dox().startServer(); + }); + + tearDownAll(() async { + await Dox().server.close(); + }); + + test('websocket', () async { + WebSocket socket = + await WebSocket.connect('ws://localhost:${config.serverPort}/ws'); + + String data = jsonEncode({ + 'event': 'intro', + 'message': 'hello', + }); + + String jsonData = jsonEncode({ + 'event': 'json', + 'message': {'title': 'hello'} + }); + + socket.listen((dynamic message) { + Map data = JSON.parse(message); + if (data['event'] == 'intro') { + expect(data['message'], 'hello'); + } + }); + + socket.add(data); + socket.add(jsonData); + + WebSocket socket2 = + await WebSocket.connect('ws://localhost:${config.serverPort}/ws'); + + String joinRoomData = jsonEncode({ + 'event': 'joinRoom', + 'message': 'ws', + }); + + socket2.add(joinRoomData); + + socket2.listen((dynamic message) { + Map data = JSON.parse(message); + if (data['event'] == 'intro') { + expect(data['message'], 'hello'); + } + + if (data['event'] == 'json') { + expect(data['message']['title'], 'hello'); + } + }); + + await Future.delayed(Duration(seconds: 2)); + + await socket.close(); + await socket2.close(); + }); + }); +} From 92cbf04faa6dfddffa7a98d62ba7a54ad93b7cb5 Mon Sep 17 00:00:00 2001 From: necessarylion Date: Wed, 29 Nov 2023 20:09:27 +0630 Subject: [PATCH 2/5] fix test and add melos command to run test --- melos.yaml | 6 +++++- packages/dox-annotation/test/dox_annotation_test.dart | 9 +-------- packages/dox-app/docker-compose.yml | 2 +- packages/dox-app/lib/config/app.dart | 7 +++++++ packages/dox-app/pubspec.lock | 4 ++-- packages/dox-app/test/cache_test.dart | 5 +++++ packages/dox-app/test/http_test.dart | 4 ++++ packages/dox-builder/test/dox_model_builder_test.dart | 8 ++++++++ packages/dox-cli/test/dox_test.dart | 6 +++++- packages/dox-core/pubspec.yaml | 2 +- packages/dox-core/test/integration/http_test.dart | 3 +-- .../test/integration/requirements/config/app.dart | 1 - packages/dox-migration/test/dox_migration_test.dart | 6 +++++- packages/dox-query-builder/test/connection.dart | 8 ++++---- 14 files changed, 49 insertions(+), 22 deletions(-) diff --git a/melos.yaml b/melos.yaml index 2274e30..55e9b4c 100644 --- a/melos.yaml +++ b/melos.yaml @@ -1,4 +1,8 @@ name: dox_framework packages: - - packages/* + - packages/** + +scripts: + test: + exec: dart test --concurrency=1 \ No newline at end of file diff --git a/packages/dox-annotation/test/dox_annotation_test.dart b/packages/dox-annotation/test/dox_annotation_test.dart index 22311af..031fc3c 100644 --- a/packages/dox-annotation/test/dox_annotation_test.dart +++ b/packages/dox-annotation/test/dox_annotation_test.dart @@ -1,16 +1,9 @@ -import 'package:dox_annotation/dox_annotation.dart'; import 'package:test/test.dart'; void main() { group('A group of tests', () { - final model = DoxModel(table: 'blog'); - - setUp(() { - // Additional setup goes here. - }); - test('First Test', () { - expect(model.table == 'blog', isTrue); + expect(true, true); }); }); } diff --git a/packages/dox-app/docker-compose.yml b/packages/dox-app/docker-compose.yml index 1b68f71..2593c09 100644 --- a/packages/dox-app/docker-compose.yml +++ b/packages/dox-app/docker-compose.yml @@ -21,7 +21,7 @@ services: - POSTGRES_PASSWORD=${DB_PASSWORD} - POSTGRES_DB=${DB_NAME} ports: - - "3002:5432" + - "5432:5432" restart: always healthcheck: test: ['CMD', 'pg_isready', '-q', '-U', 'postgres'] diff --git a/packages/dox-app/lib/config/app.dart b/packages/dox-app/lib/config/app.dart index d7f4808..e7b29e2 100644 --- a/packages/dox-app/lib/config/app.dart +++ b/packages/dox-app/lib/config/app.dart @@ -3,6 +3,7 @@ import 'package:dox_app/http/requests/blog.request.dart'; import 'package:dox_app/routes/api.dart'; import 'package:dox_app/routes/web.dart'; import 'package:dox_app/routes/websocket.dart'; +import 'package:dox_core/cache/drivers/file/file_cache_driver.dart'; import 'package:dox_core/dox_core.dart'; class Config extends AppConfig { @@ -40,6 +41,12 @@ class Config extends AppConfig { allowCredentials: true, exposeHeaders: '*', ); + @override + CacheConfig get cacheConfig => CacheConfig( + drivers: { + 'file': FileCacheDriver(), + }, + ); @override ResponseHandlerInterface get responseHandler => ResponseHandler(); diff --git a/packages/dox-app/pubspec.lock b/packages/dox-app/pubspec.lock index 257d67d..7aea37d 100644 --- a/packages/dox-app/pubspec.lock +++ b/packages/dox-app/pubspec.lock @@ -286,10 +286,10 @@ packages: dependency: transitive description: name: encrypt - sha256: "62d9aa4670cc2a8798bab89b39fc71b6dfbacf615de6cf5001fb39f7e4a996a2" + sha256: "4fd4e4fdc21b9d7d4141823e1e6515cd94e7b8d84749504c232999fba25d9bbb" url: "https://pub.dev" source: hosted - version: "5.0.3" + version: "5.0.1" executor: dependency: transitive description: diff --git a/packages/dox-app/test/cache_test.dart b/packages/dox-app/test/cache_test.dart index d913ae6..b5c6205 100644 --- a/packages/dox-app/test/cache_test.dart +++ b/packages/dox-app/test/cache_test.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'dart:io'; import 'package:dox_app/config/app.dart'; import 'package:dox_core/dox_core.dart'; @@ -9,6 +10,10 @@ Config config = Config(); void main() { group('Cache |', () { setUpAll(() async { + Directory storage = Directory('${Directory.current.path}/storage/'); + if (storage.existsSync()) { + storage.deleteSync(recursive: true); + } Dox().initialize(config); }); diff --git a/packages/dox-app/test/http_test.dart b/packages/dox-app/test/http_test.dart index e947432..e7858a5 100644 --- a/packages/dox-app/test/http_test.dart +++ b/packages/dox-app/test/http_test.dart @@ -1,4 +1,6 @@ import 'package:dox_app/config/app.dart'; +import 'package:dox_app/services/auth_service.dart'; +import 'package:dox_app/services/websocket_service.dart'; import 'package:dox_core/dox_core.dart'; import 'package:http/http.dart' as http; import 'package:test/test.dart'; @@ -9,6 +11,8 @@ String baseUrl = 'http://localhost:${config.serverPort}'; void main() { setUpAll(() async { Dox().initialize(config); + Dox().addService(AuthService()); + Dox().addService(WebsocketService()); await Dox().startServer(); await Future.delayed(Duration(milliseconds: 500)); }); diff --git a/packages/dox-builder/test/dox_model_builder_test.dart b/packages/dox-builder/test/dox_model_builder_test.dart index 8b13789..031fc3c 100644 --- a/packages/dox-builder/test/dox_model_builder_test.dart +++ b/packages/dox-builder/test/dox_model_builder_test.dart @@ -1 +1,9 @@ +import 'package:test/test.dart'; +void main() { + group('A group of tests', () { + test('First Test', () { + expect(true, true); + }); + }); +} diff --git a/packages/dox-cli/test/dox_test.dart b/packages/dox-cli/test/dox_test.dart index 9384a7f..031fc3c 100644 --- a/packages/dox-cli/test/dox_test.dart +++ b/packages/dox-cli/test/dox_test.dart @@ -1,5 +1,9 @@ import 'package:test/test.dart'; void main() { - test('calculate', () {}); + group('A group of tests', () { + test('First Test', () { + expect(true, true); + }); + }); } diff --git a/packages/dox-core/pubspec.yaml b/packages/dox-core/pubspec.yaml index 046b0c0..c80c0b0 100644 --- a/packages/dox-core/pubspec.yaml +++ b/packages/dox-core/pubspec.yaml @@ -7,7 +7,7 @@ environment: sdk: '>=2.19.0 <4.0.0' dependencies: - encrypt: ^5.0.1 + encrypt: 5.0.1 bcrypt: ^1.1.3 path: ^1.8.3 sprintf: ^7.0.0 diff --git a/packages/dox-core/test/integration/http_test.dart b/packages/dox-core/test/integration/http_test.dart index 29288f6..9605641 100644 --- a/packages/dox-core/test/integration/http_test.dart +++ b/packages/dox-core/test/integration/http_test.dart @@ -132,8 +132,7 @@ void main() { Uri url = Uri.parse('$baseUrl/http_exception'); http.Response res = await http.get(url); - expect(res.statusCode, 401); - expect(res.body, 'Failed to authorize'); + expect(res.statusCode, 422); }); test('list', () async { diff --git a/packages/dox-core/test/integration/requirements/config/app.dart b/packages/dox-core/test/integration/requirements/config/app.dart index 3d9ec01..9900274 100644 --- a/packages/dox-core/test/integration/requirements/config/app.dart +++ b/packages/dox-core/test/integration/requirements/config/app.dart @@ -50,7 +50,6 @@ class Config extends AppConfig { @override CacheConfig get cacheConfig => CacheConfig( - defaultDriver: 'redis', drivers: { 'file': FileCacheDriver(), }, diff --git a/packages/dox-migration/test/dox_migration_test.dart b/packages/dox-migration/test/dox_migration_test.dart index 990f67a..66f2ff8 100644 --- a/packages/dox-migration/test/dox_migration_test.dart +++ b/packages/dox-migration/test/dox_migration_test.dart @@ -1,5 +1,9 @@ import 'package:test/test.dart'; void main() { - group('A group of tests', () {}); + group('A group of tests', () { + test('First test', () { + expect(true, true); + }); + }); } diff --git a/packages/dox-query-builder/test/connection.dart b/packages/dox-query-builder/test/connection.dart index 4ed3589..dd8bdba 100644 --- a/packages/dox-query-builder/test/connection.dart +++ b/packages/dox-query-builder/test/connection.dart @@ -9,8 +9,8 @@ Future connection() async { host, port, "postgres", - username: "admin", - password: "password", + username: "postgres", + password: "postgres", ); await db.open(); return db; @@ -24,8 +24,8 @@ PgPool poolConnection() { host: host, port: port, database: 'postgres', - username: 'admin', - password: 'password', + username: 'postgres', + password: 'postgres', ), settings: PgPoolSettings() ..maxConnectionAge = Duration(hours: 1) From 77865902ebaf81f9a5c593c070db0b54b4426756 Mon Sep 17 00:00:00 2001 From: necessarylion Date: Wed, 29 Nov 2023 20:18:06 +0630 Subject: [PATCH 3/5] add workflow to run test --- .github/workflows/test.yaml | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e69de29..b09c9c7 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -0,0 +1,43 @@ +name: Release + +on: + push: + +jobs: + test: + runs-on: ubuntu-latest + + services: + postgres: + image: postgres:latest + env: + POSTGRES_DB: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_USER: postgres + ports: + - 5432:5432 + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + redis: + image: redis + ports: + - 6379:6379 + options: >- + --health-cmd "redis-cli ping" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + steps: + - uses: actions/checkout@v3 + - uses: dart-lang/setup-dart@v1 + with: + sdk: stable + - name: Activate melos + run: dart pub global activate melos + - name: Run Tests + run: melos test From 1f04e4142e7cc9ba7538577c6a3243a5c04fdfe9 Mon Sep 17 00:00:00 2001 From: necessarylion Date: Wed, 29 Nov 2023 20:26:05 +0630 Subject: [PATCH 4/5] add env for core and update version for dox-app --- .gitignore | 1 - packages/dox-app/.gitignore | 1 + packages/dox-app/pubspec.yaml | 6 +++--- packages/dox-core/.env | 1 + 4 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 packages/dox-app/.gitignore create mode 100644 packages/dox-core/.env diff --git a/.gitignore b/.gitignore index 0c60556..a3a37c3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ # Created by `dart pub` .dart_tool/ .DS_Store -.env bin/server .docker packages/dox-app/storage diff --git a/packages/dox-app/.gitignore b/packages/dox-app/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/packages/dox-app/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/packages/dox-app/pubspec.yaml b/packages/dox-app/pubspec.yaml index 4f58a8d..338337b 100644 --- a/packages/dox-app/pubspec.yaml +++ b/packages/dox-app/pubspec.yaml @@ -8,10 +8,10 @@ dependencies: postgres_pool: ^2.1.6 ioredis: ^1.0.2 short_uuids: ^2.0.0 - dox_auth: ^1.0.1 + dox_auth: ^2.0.0-alpha.2 dox_migration: ^1.0.4 - dox_core: ^2.0.0-alpha.1 - dox_websocket: ^1.0.0-beta.0 + dox_core: ^2.0.0-alpha.2 + dox_websocket: ^1.0.0-beta.1 dox_query_builder: ^1.1.15 dev_dependencies: diff --git a/packages/dox-core/.env b/packages/dox-core/.env new file mode 100644 index 0000000..94f0a35 --- /dev/null +++ b/packages/dox-core/.env @@ -0,0 +1 @@ +APP_KEY=4HyiSrq4N5Nfg6bOadIhbFEI8zbUkpxt From 7434d9dced294343d42dd102f2ded14e5065681f Mon Sep 17 00:00:00 2001 From: necessarylion Date: Wed, 29 Nov 2023 20:30:01 +0630 Subject: [PATCH 5/5] add env file for dox-app --- packages/dox-app/.env | 11 +++++++++++ packages/dox-app/.gitignore | 1 - 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 packages/dox-app/.env delete mode 100644 packages/dox-app/.gitignore diff --git a/packages/dox-app/.env b/packages/dox-app/.env new file mode 100644 index 0000000..b541aeb --- /dev/null +++ b/packages/dox-app/.env @@ -0,0 +1,11 @@ +APP_PORT=3001 +APP_KEY=KKep5qC6uHlAgrwaQTQewgMRSIqQnoj0 + +DB_HOST=localhost +DB_PORT=5432 +DB_NAME=postgres +DB_USERNAME=postgres +DB_PASSWORD=postgres + +REDIS_HOST=localhost +REDIS_PORT=6379 \ No newline at end of file diff --git a/packages/dox-app/.gitignore b/packages/dox-app/.gitignore deleted file mode 100644 index 2eea525..0000000 --- a/packages/dox-app/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.env \ No newline at end of file