Skip to content

Commit

Permalink
added unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bsutton committed Jul 19, 2022
1 parent 9d5bb6e commit e58e4eb
Show file tree
Hide file tree
Showing 45 changed files with 893 additions and 451 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"request": "launch",
"program": "bin/onepub.dart",
"args": ["import"],
"env": {"ONEPUB_SECRET" : "ABCDE"}
"env": {"ONEPUB_TOKEN" : "ABCDE"}
},

{
Expand Down
1 change: 1 addition & 0 deletions dart_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exclude_tags: manual
71 changes: 71 additions & 0 deletions lib/src/api/api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import 'dart:io';

import '../util/send_command.dart';
import 'logout.dart';
import 'onepub_token.dart';
import 'organisation.dart';
import 'status.dart';

class API {
Future<Status> status() async {
try {
const endpoint = '/status';

final response = await sendCommand(command: endpoint, authorised: false);

return Status(response.status, response.data['message']! as String);
} on IOException {
return Status(500, 'Connection failed');
}
}

Future<Logout> logout() async {
const endpoint = '/member/logout';
final response = await sendCommand(command: endpoint);

return Logout(response);
}

/// Fetches the [OnePubToken] for the member whos email
/// address is [memberEmail].
/// The member must be logged in to the cli.
Future<OnePubToken> fetchMemberToken(String memberEmail) async {
final endpoint = 'member/token/$memberEmail';
final response = await sendCommand(command: endpoint);

return OnePubToken(response);
}

/// Fetches the organisation details associated with the [onepubToken]
Future<Organisation> fetchOrganisation(String onepubToken) async {
// the import is an alternate (from login) form of getting
// authorised but we have a chicken and egg problem
// because the [sendCommand] expects the token to be
// in the token store which it isn't
// So we paass the auth header directly.
final headers = <String, String>{}..addAll({'authorization': onepubToken});

const endpoint = '/organisation/token';

final response = await sendCommand(
command: endpoint, authorised: false, headers: headers);

return Organisation(response);
}

Future<Organisation> fetchOrganisationById(String obfuscatedId) async {
final endpoint = 'organisation/$obfuscatedId';
final response = await sendCommand(command: endpoint);

/// we push the id into the map so we can share a common
/// constructor with [fetchOrganisation]
response.data['obfuscatedId'] = obfuscatedId;
return Organisation(response);
}

/// Creates a (empty) package owned by [team]
Future<void> createPackage(String packageName, String team) async {
final endpoint = 'package/create/$packageName/team/$team';
await sendCommand(command: endpoint);
}
}
30 changes: 30 additions & 0 deletions lib/src/api/logout.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import '../util/send_command.dart';

class Logout {
Logout(EndpointResponse response) {
success = response.success;

if (!success) {
errorMessage = response.data['message']! as String;
// if we failed because we were already logged out
// we still report success.
if (errorMessage!.startsWith('Your token is no longer valid') ||
errorMessage!
.startsWith('You must be logged in to run this command.')) {
wasAlreadyLoggedOut = true;
errorMessage = null;
success = true;
}
} else {
wasAlreadyLoggedOut = false;
}
}

late final bool success;

/// If the call failed this contains the error message.
late final String? errorMessage;

/// If the user was already logged out when we called logout.
late final bool wasAlreadyLoggedOut;
}
18 changes: 18 additions & 0 deletions lib/src/api/onepub_token.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import '../util/send_command.dart';

class OnePubToken {
OnePubToken(EndpointResponse response) {
if (response.success) {
token = response.data['onepubToken'] as String?;
}

if (token == null) {
errorMessage = response.data['message']! as String;
}
}

bool get success => token != null;
String? token;

String? errorMessage;
}
34 changes: 34 additions & 0 deletions lib/src/api/organisation.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'dart:io';

import '../util/send_command.dart';

class Organisation {
Organisation(EndpointResponse response) {
_success = response.success;

if (response.status == HttpStatus.notFound) {
notFound = true;
}

if (!response.success) {
errorMessage = response.data['message']! as String;
}

name = response.data['organisationName']! as String;
obfuscatedId = response.data['obfuscatedId']! as String;
}

late final bool _success;
late final String name;
late final String obfuscatedId;

bool get success => _success;

/// If success is false then you can check this field
/// to see if it failed because the organisation wasn't found
/// if this is false then a more serious error occured
bool notFound = false;

/// if [success] is false this will contain the error message.
late final String? errorMessage;
}
6 changes: 6 additions & 0 deletions lib/src/api/status.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Status {
Status(this.statusCode, this.message);

int statusCode;
String message;
}
48 changes: 21 additions & 27 deletions lib/src/commands/doctor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import 'dart:io';
import 'package:args/command_runner.dart';
import 'package:dcli/dcli.dart';

import '../api/api.dart';
import '../onepub_settings.dart';
import '../util/log.dart';
import '../util/one_pub_token_store.dart';
import '../util/send_command.dart';

///
class DoctorCommand extends Command<int> {
Expand All @@ -27,28 +26,24 @@ class DoctorCommand extends Command<int> {

@override
int run() {
if (!exists(OnePubSettings.pathToSettings)) {
logerr(red('''Something went wrong, could not find settings file.'''));
exit(1);
}
OnePubSettings.load();

print(blue('Dart'));
print('Dart version: ${DartSdk().version}');
print('Dart path: ${DartSdk().pathToDartExe}');
withSettings(() {
print(blue('Dart'));
print('Dart version: ${DartSdk().version}');
print('Dart path: ${DartSdk().pathToDartExe}');

print(blue('\nURLs'));
print('Web site: ${OnePubSettings().onepubWebUrl}');
print('API endpoint: ${OnePubSettings().onepubApiUrl}');
print(blue('\nURLs'));
print('Web site: ${OnePubSettings.use.onepubWebUrl}');
print('API endpoint: ${OnePubSettings.use.onepubApiUrl}');

print(blue('\nEnvironment'));
envStatus('PUB_CACHE');
envStatus('PATH');
print(blue('\nEnvironment'));
envStatus('PUB_CACHE');
envStatus('PATH');

tokenStatus();
tokenStatus();

print('');
_status();
print('');
_status();
});
return 0;
}

Expand All @@ -64,25 +59,24 @@ class DoctorCommand extends Command<int> {
print(blue('Status'));
if (OnePubTokenStore().isLoggedIn) {
print('Logged In: true');
print('Member: ${OnePubSettings().operatorEmail}');
print('Organisation: ${OnePubSettings().organisationName}');
print('Member: ${OnePubSettings.use.operatorEmail}');
print('Organisation: ${OnePubSettings.use.organisationName}');
} else {
print(orange('''
You are not logged into OnePub.
run: onepub login'''));
}
try {
const endpoint = '/status';
echo('checking status... ');

final response = await sendCommand(command: endpoint, authorised: false);
final status = await API().status();

if (response.status == 200) {
if (status.statusCode == 200) {
print('');
print(green(response.data['message']! as String));
print(green(status.message));
} else {
print('');
print(red(response.data['message']! as String));
print(red(status.message));
}
} on IOException catch (e) {
printerr(red(e.toString()));
Expand Down
Loading

0 comments on commit e58e4eb

Please sign in to comment.