Skip to content

Commit

Permalink
updated logs and settings services
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Aug 21, 2022
1 parent bdeb049 commit 20ce16f
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 6 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.3.0

- Renamed `LogRequestModel.ip` to `LogRequestModel.remoteIp`.
- Added `LogRequestModel.userIp` (the "real" user ip when behind a reverse proxy).
- Added `SettingsService.testS3()` to test the S3 storage connection.
- Added `SettingsService.testEmail()` to send a test email.


## 0.2.0

- Added `CollectionService.import()`.
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,12 @@ final client = PocketBase("http://127.0.0.1:8090", authStore: CustomAuthStore())
// Bulk updates app settings.
🔐 client.settings.update({body, query, headers});
// Performs a S3 storage connection test.
🔐 client.settings.testS3({body, query, headers});
// Sends a test email (verification, password-reset, email-change).
🔐 client.settings.testEmail(toEmail, template, {body, query, headers});
```


Expand Down
6 changes: 4 additions & 2 deletions lib/src/dtos/log_request_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class LogRequestModel implements Jsonable {
String method;
int status;
String auth;
String ip;
String remoteIp;
String userIp;
String referer;
String userAgent;
Map<String, dynamic> meta;
Expand All @@ -29,7 +30,8 @@ class LogRequestModel implements Jsonable {
this.method = "",
this.status = 0,
this.auth = "",
this.ip = "",
this.remoteIp = "",
this.userIp = "",
this.referer = "",
this.userAgent = "",
this.meta = const {},
Expand Down
6 changes: 4 additions & 2 deletions lib/src/dtos/log_request_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions lib/src/services/settings_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,45 @@ class SettingsService extends BaseService {
)
.then((data) => data as Map<String, dynamic>? ?? {});
}

/// Performs a S3 storage connection test.
Future<void> testS3({
Map<String, dynamic> body = const {},
Map<String, dynamic> query = const {},
Map<String, String> headers = const {},
}) {
return client.send(
"/api/settings/test/s3",
method: "POST",
body: body,
query: query,
headers: headers,
);
}

/// Sends a test email.
///
/// The possible `template` values are:
/// - verification
/// - password-reset
/// - email-change
Future<void> testEmail(
String toEmail,
String template, {
Map<String, dynamic> body = const {},
Map<String, dynamic> query = const {},
Map<String, String> headers = const {},
}) {
final enrichedBody = Map<String, dynamic>.of(body);
enrichedBody["email"] ??= toEmail;
enrichedBody["template"] ??= template;

return client.send(
"/api/settings/test/email",
method: "POST",
body: enrichedBody,
query: query,
headers: headers,
);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: pocketbase
description: Multi-platform Dart SDK for PocketBase, an open source realtime backend in 1 file.
repository: https://github.com/pocketbase/dart-sdk

version: 0.2.0
version: 0.3.0

environment:
sdk: '>=2.14.0 <3.0.0'
Expand Down
3 changes: 2 additions & 1 deletion test/dtos/log_request_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ void main() {
"method": "test_method",
"status": 123,
"auth": "test_auth",
"ip": "test_ip",
"remoteIp": "test_ip",
"userIp": "test_ip",
"referer": "test_referer",
"userAgent": "test_userAgent",
"meta": {"a": 123},
Expand Down
72 changes: 72 additions & 0 deletions test/services/settings_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,77 @@ void main() {
equals({"a": 1, "b": false, "c": "test"}),
);
});

test("testS3()", () async {
final mock = MockClient((request) async {
expect(request.method, "POST");
expect(request.body, jsonEncode({"test_body": 123}));
expect(
request.url.toString(),
"/base/api/settings/test/s3?a=1&a=2&b=%40demo",
);
expect(request.headers["test"], "789");

return http.Response(
jsonEncode({"a": 1, "b": false, "c": "test"}),
200,
);
});

final client = PocketBase("/base", httpClientFactory: () => mock);

await client.settings.testS3(
query: {
"a": ["1", null, 2],
"b": "@demo",
},
body: {
"test_body": 123,
},
headers: {
"test": "789",
},
);
});

test("testEmail()", () async {
final mock = MockClient((request) async {
expect(request.method, "POST");
expect(
request.body,
jsonEncode({
"test_body": 123,
"email": "test@example.com",
"template": "test_template",
}));
expect(
request.url.toString(),
"/base/api/settings/test/email?a=1&a=2&b=%40demo",
);
expect(request.headers["test"], "789");

return http.Response(
jsonEncode({"a": 1, "b": false, "c": "test"}),
200,
);
});

final client = PocketBase("/base", httpClientFactory: () => mock);

await client.settings.testEmail(
"test@example.com",
"test_template",
query: {
"a": ["1", null, 2],
"b": "@demo",
},
body: {
"test_body": 123,
},
headers: {
"test": "789",
},
);
});
});
}

0 comments on commit 20ce16f

Please sign in to comment.