mirror of
https://github.com/foss42/apidash.git
synced 2025-12-10 07:08:08 +08:00
tests: add api auth model tests(coverage 100%)
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
import 'package:better_networking/models/auth/api_auth_model.dart';
|
||||
import 'package:better_networking/models/auth/auth_basic_model.dart';
|
||||
import 'package:better_networking/models/auth/auth_bearer_model.dart';
|
||||
import 'package:better_networking/consts.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'auth_models.dart';
|
||||
|
||||
void main() {
|
||||
group('Testing AuthModel (API Auth Model)', () {
|
||||
test("Testing AuthModel copyWith", () {
|
||||
var authModel = authModel1;
|
||||
final authModelCopyWith = authModel.copyWith(
|
||||
type: APIAuthType.bearer,
|
||||
bearer: const AuthBearerModel(token: 'new-bearer-token'),
|
||||
basic: null,
|
||||
);
|
||||
expect(authModelCopyWith.type, APIAuthType.bearer);
|
||||
expect(authModelCopyWith.bearer?.token, 'new-bearer-token');
|
||||
expect(authModelCopyWith.basic, null);
|
||||
// original model unchanged
|
||||
expect(authModel.type, APIAuthType.basic);
|
||||
expect(authModel.basic?.username, 'john_doe');
|
||||
});
|
||||
|
||||
test("Testing AuthModel toJson", () {
|
||||
var authModel = authModel1;
|
||||
expect(authModel.toJson(), authModelJson1);
|
||||
});
|
||||
|
||||
test("Testing AuthModel fromJson for basic authentication", () {
|
||||
var authModel = authModel1;
|
||||
final modelFromJson = AuthModel.fromJson(authModelJson1);
|
||||
expect(modelFromJson, authModel);
|
||||
expect(modelFromJson.type, APIAuthType.basic);
|
||||
expect(modelFromJson.basic?.username, 'john_doe');
|
||||
expect(modelFromJson.basic?.password, 'secure_password');
|
||||
});
|
||||
|
||||
test("Testing AuthModel fromJson for bearer authentication", () {
|
||||
var authModel = authModel2;
|
||||
final modelFromJson = AuthModel.fromJson(authModelJson2);
|
||||
expect(modelFromJson, authModel);
|
||||
expect(modelFromJson.type, APIAuthType.bearer);
|
||||
expect(
|
||||
modelFromJson.bearer?.token,
|
||||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
|
||||
);
|
||||
});
|
||||
|
||||
test("Testing AuthModel fromJson for api key authentication", () {
|
||||
var authModel = authModel3;
|
||||
final apiKeyModelJson = {
|
||||
"type": "apiKey",
|
||||
"apikey": authApiKeyModelJson1,
|
||||
"bearer": null,
|
||||
"basic": null,
|
||||
"jwt": null,
|
||||
"digest": null,
|
||||
};
|
||||
final modelFromJson = AuthModel.fromJson(apiKeyModelJson);
|
||||
expect(modelFromJson, authModel);
|
||||
expect(modelFromJson.type, APIAuthType.apiKey);
|
||||
expect(modelFromJson.apikey?.key, 'ak-test-key-12345');
|
||||
expect(modelFromJson.apikey?.location, 'header');
|
||||
expect(modelFromJson.apikey?.name, 'x-api-key');
|
||||
});
|
||||
|
||||
test("Testing AuthModel fromJson for jwt authentication", () {
|
||||
var authModel = authModel4;
|
||||
final jwtModelJson = {
|
||||
"type": "jwt",
|
||||
"apikey": null,
|
||||
"bearer": null,
|
||||
"basic": null,
|
||||
"jwt": authJwtModelJson1,
|
||||
"digest": null,
|
||||
};
|
||||
final modelFromJson = AuthModel.fromJson(jwtModelJson);
|
||||
expect(modelFromJson, authModel);
|
||||
expect(modelFromJson.type, APIAuthType.jwt);
|
||||
expect(modelFromJson.jwt?.secret, 'jwt-secret-key');
|
||||
expect(modelFromJson.jwt?.algorithm, 'RS256');
|
||||
expect(modelFromJson.jwt?.isSecretBase64Encoded, true);
|
||||
expect(modelFromJson.jwt?.headerPrefix, 'JWT');
|
||||
});
|
||||
|
||||
test("Testing AuthModel fromJson for digest authentication", () {
|
||||
var authModel = authModel5;
|
||||
final digestModelJson = {
|
||||
"type": "digest",
|
||||
"apikey": null,
|
||||
"bearer": null,
|
||||
"basic": null,
|
||||
"jwt": null,
|
||||
"digest": authDigestModelJson1,
|
||||
};
|
||||
final modelFromJson = AuthModel.fromJson(digestModelJson);
|
||||
expect(modelFromJson, authModel);
|
||||
expect(modelFromJson.type, APIAuthType.digest);
|
||||
expect(modelFromJson.digest?.algorithm, 'SHA-256');
|
||||
expect(modelFromJson.digest?.username, 'digest_user');
|
||||
expect(modelFromJson.digest?.password, 'digest_pass');
|
||||
expect(modelFromJson.digest?.realm, 'protected-area');
|
||||
expect(modelFromJson.digest?.qop, 'auth-int');
|
||||
});
|
||||
|
||||
test("Testing AuthModel getters for different auth types", () {
|
||||
expect(authModelNone.type, APIAuthType.none);
|
||||
expect(authModel1.type, APIAuthType.basic);
|
||||
expect(authModel2.type, APIAuthType.bearer);
|
||||
expect(authModel3.type, APIAuthType.apiKey);
|
||||
expect(authModel4.type, APIAuthType.jwt);
|
||||
expect(authModel5.type, APIAuthType.digest);
|
||||
});
|
||||
|
||||
test("Testing AuthModel with basic authentication", () {
|
||||
var authModel = authModel1;
|
||||
expect(authModel.type, APIAuthType.basic);
|
||||
expect(authModel.basic, isNotNull);
|
||||
expect(authModel.basic?.username, 'john_doe');
|
||||
expect(authModel.basic?.password, 'secure_password');
|
||||
expect(authModel.bearer, null);
|
||||
expect(authModel.apikey, null);
|
||||
expect(authModel.jwt, null);
|
||||
expect(authModel.digest, null);
|
||||
});
|
||||
|
||||
test("Testing AuthModel with bearer authentication", () {
|
||||
var authModel = authModel2;
|
||||
expect(authModel.type, APIAuthType.bearer);
|
||||
expect(authModel.bearer, isNotNull);
|
||||
expect(
|
||||
authModel.bearer?.token,
|
||||
startsWith('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'),
|
||||
);
|
||||
expect(authModel.basic, null);
|
||||
expect(authModel.apikey, null);
|
||||
expect(authModel.jwt, null);
|
||||
expect(authModel.digest, null);
|
||||
});
|
||||
|
||||
test("Testing AuthModel with API key authentication", () {
|
||||
var authModel = authModel3;
|
||||
expect(authModel.type, APIAuthType.apiKey);
|
||||
expect(authModel.apikey, isNotNull);
|
||||
expect(authModel.apikey?.key, 'ak-test-key-12345');
|
||||
expect(authModel.apikey?.location, 'header');
|
||||
expect(authModel.apikey?.name, 'x-api-key');
|
||||
expect(authModel.basic, null);
|
||||
expect(authModel.bearer, null);
|
||||
expect(authModel.jwt, null);
|
||||
expect(authModel.digest, null);
|
||||
});
|
||||
|
||||
test("Testing AuthModel with JWT authentication", () {
|
||||
var authModel = authModel4;
|
||||
expect(authModel.type, APIAuthType.jwt);
|
||||
expect(authModel.jwt, isNotNull);
|
||||
expect(authModel.jwt?.secret, 'jwt-secret-key');
|
||||
expect(authModel.jwt?.algorithm, 'RS256');
|
||||
expect(authModel.jwt?.isSecretBase64Encoded, true);
|
||||
expect(authModel.basic, null);
|
||||
expect(authModel.bearer, null);
|
||||
expect(authModel.apikey, null);
|
||||
expect(authModel.digest, null);
|
||||
});
|
||||
|
||||
test("Testing AuthModel with digest authentication", () {
|
||||
var authModel = authModel5;
|
||||
expect(authModel.type, APIAuthType.digest);
|
||||
expect(authModel.digest, isNotNull);
|
||||
expect(authModel.digest?.username, 'digest_user');
|
||||
expect(authModel.digest?.algorithm, 'SHA-256');
|
||||
expect(authModel.digest?.qop, 'auth-int');
|
||||
expect(authModel.basic, null);
|
||||
expect(authModel.bearer, null);
|
||||
expect(authModel.apikey, null);
|
||||
expect(authModel.jwt, null);
|
||||
});
|
||||
|
||||
test("Testing AuthModel with none authentication", () {
|
||||
var authModel = authModelNone;
|
||||
expect(authModel.type, APIAuthType.none);
|
||||
expect(authModel.basic, null);
|
||||
expect(authModel.bearer, null);
|
||||
expect(authModel.apikey, null);
|
||||
expect(authModel.jwt, null);
|
||||
expect(authModel.digest, null);
|
||||
});
|
||||
|
||||
test("Testing AuthModel equality", () {
|
||||
const authModel1Copy = AuthModel(
|
||||
type: APIAuthType.basic,
|
||||
basic: AuthBasicAuthModel(
|
||||
username: 'john_doe',
|
||||
password: 'secure_password',
|
||||
),
|
||||
);
|
||||
expect(authModel1, authModel1Copy);
|
||||
expect(authModel1, isNot(authModel2));
|
||||
expect(authModelNone, const AuthModel(type: APIAuthType.none));
|
||||
});
|
||||
|
||||
test("Testing AuthModel JSON serialization for different types", () {
|
||||
var bearerModel = authModel2;
|
||||
var bearerJson = bearerModel.toJson();
|
||||
expect(bearerJson['type'], 'bearer');
|
||||
expect(bearerJson['bearer'], isNotNull);
|
||||
expect(bearerJson['basic'], null);
|
||||
|
||||
final modelFromJson = AuthModel.fromJson(authModelJson2);
|
||||
expect(modelFromJson, bearerModel);
|
||||
});
|
||||
|
||||
test("Testing AuthModel JSON serialization for none type", () {
|
||||
var noneModel = authModelNone;
|
||||
var noneJson = noneModel.toJson();
|
||||
expect(noneJson, authModelNoneJson);
|
||||
|
||||
final modelFromJson = AuthModel.fromJson(authModelNoneJson);
|
||||
expect(modelFromJson, noneModel);
|
||||
expect(modelFromJson.type, APIAuthType.none);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import 'package:better_networking/models/auth/auth_api_key_model.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'auth_models.dart';
|
||||
|
||||
void main() {
|
||||
group('Testing AuthApiKeyModel', () {
|
||||
test("Testing AuthApiKeyModel copyWith", () {
|
||||
var authApiKeyModel = authApiKeyModel1;
|
||||
final authApiKeyModelCopyWith = authApiKeyModel.copyWith(
|
||||
key: 'new_api_key',
|
||||
location: 'query',
|
||||
);
|
||||
expect(authApiKeyModelCopyWith.key, 'new_api_key');
|
||||
expect(authApiKeyModelCopyWith.location, 'query');
|
||||
// original model unchanged
|
||||
expect(authApiKeyModel.key, 'ak-test-key-12345');
|
||||
expect(authApiKeyModel.location, 'header');
|
||||
expect(authApiKeyModel.name, 'x-api-key');
|
||||
});
|
||||
|
||||
test("Testing AuthApiKeyModel toJson", () {
|
||||
var authApiKeyModel = authApiKeyModel1;
|
||||
expect(authApiKeyModel.toJson(), authApiKeyModelJson1);
|
||||
});
|
||||
|
||||
test("Testing AuthApiKeyModel fromJson", () {
|
||||
var authApiKeyModel = authApiKeyModel1;
|
||||
final modelFromJson = AuthApiKeyModel.fromJson(authApiKeyModelJson1);
|
||||
expect(modelFromJson, authApiKeyModel);
|
||||
expect(modelFromJson.key, 'ak-test-key-12345');
|
||||
expect(modelFromJson.location, 'header');
|
||||
expect(modelFromJson.name, 'x-api-key');
|
||||
});
|
||||
|
||||
test("Testing AuthApiKeyModel getters", () {
|
||||
var authApiKeyModel = authApiKeyModel1;
|
||||
expect(authApiKeyModel.key, 'ak-test-key-12345');
|
||||
expect(authApiKeyModel.location, 'header');
|
||||
expect(authApiKeyModel.name, 'x-api-key');
|
||||
});
|
||||
|
||||
test("Testing AuthApiKeyModel default values", () {
|
||||
const authApiKeyModelMinimal = AuthApiKeyModel(key: 'test-key');
|
||||
expect(authApiKeyModelMinimal.key, 'test-key');
|
||||
expect(authApiKeyModelMinimal.location, 'header'); // default value
|
||||
expect(authApiKeyModelMinimal.name, 'x-api-key'); // default value
|
||||
});
|
||||
|
||||
test("Testing AuthApiKeyModel equality", () {
|
||||
const authApiKeyModel1Copy = AuthApiKeyModel(
|
||||
key: 'ak-test-key-12345',
|
||||
location: 'header',
|
||||
name: 'x-api-key',
|
||||
);
|
||||
expect(authApiKeyModel1, authApiKeyModel1Copy);
|
||||
expect(authApiKeyModel1, isNot(authApiKeyModel2));
|
||||
});
|
||||
|
||||
test("Testing AuthApiKeyModel with different configurations", () {
|
||||
expect(authApiKeyModel2.key, 'query-api-key-67890');
|
||||
expect(authApiKeyModel2.location, 'query');
|
||||
expect(authApiKeyModel2.name, 'api_key');
|
||||
expect(authApiKeyModel1.location, isNot(authApiKeyModel2.location));
|
||||
expect(authApiKeyModel1.name, isNot(authApiKeyModel2.name));
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import 'package:better_networking/models/auth/auth_basic_model.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'auth_models.dart';
|
||||
|
||||
void main() {
|
||||
group('Testing AuthBasicAuthModel', () {
|
||||
test("Testing AuthBasicAuthModel copyWith", () {
|
||||
var authBasicModel = authBasicModel1;
|
||||
final authBasicModelCopyWith = authBasicModel.copyWith(
|
||||
password: 'new_password',
|
||||
);
|
||||
expect(authBasicModelCopyWith.password, 'new_password');
|
||||
// original model unchanged
|
||||
expect(authBasicModel.username, 'john_doe');
|
||||
expect(authBasicModel.password, 'secure_password');
|
||||
});
|
||||
|
||||
test("Testing AuthBasicAuthModel toJson", () {
|
||||
var authBasicModel = authBasicModel1;
|
||||
expect(authBasicModel.toJson(), authBasicModelJson1);
|
||||
});
|
||||
|
||||
test("Testing AuthBasicAuthModel fromJson", () {
|
||||
var authBasicModel = authBasicModel1;
|
||||
final modelFromJson = AuthBasicAuthModel.fromJson(authBasicModelJson1);
|
||||
expect(modelFromJson, authBasicModel);
|
||||
expect(modelFromJson.username, 'john_doe');
|
||||
expect(modelFromJson.password, 'secure_password');
|
||||
});
|
||||
|
||||
test("Testing AuthBasicAuthModel getters", () {
|
||||
var authBasicModel = authBasicModel1;
|
||||
expect(authBasicModel.username, 'john_doe');
|
||||
expect(authBasicModel.password, 'secure_password');
|
||||
});
|
||||
|
||||
test("Testing AuthBasicAuthModel equality", () {
|
||||
const authBasicModel1Copy = AuthBasicAuthModel(
|
||||
username: 'john_doe',
|
||||
password: 'secure_password',
|
||||
);
|
||||
expect(authBasicModel1, authBasicModel1Copy);
|
||||
expect(authBasicModel1, isNot(authBasicModel2));
|
||||
});
|
||||
|
||||
test("Testing AuthBasicAuthModel with different values", () {
|
||||
expect(authBasicModel2.username, 'jane_smith');
|
||||
expect(authBasicModel2.password, 'another_password');
|
||||
expect(authBasicModel1.username, isNot(authBasicModel2.username));
|
||||
expect(authBasicModel1.password, isNot(authBasicModel2.password));
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import 'package:better_networking/models/auth/auth_bearer_model.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'auth_models.dart';
|
||||
|
||||
void main() {
|
||||
group('Testing AuthBearerModel', () {
|
||||
test("Testing AuthBearerModel copyWith", () {
|
||||
var authBearerModel = authBearerModel1;
|
||||
final authBearerModelCopyWith = authBearerModel.copyWith(
|
||||
token: 'new_bearer_token',
|
||||
);
|
||||
expect(authBearerModelCopyWith.token, 'new_bearer_token');
|
||||
// original model unchanged
|
||||
expect(
|
||||
authBearerModel.token,
|
||||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
|
||||
);
|
||||
});
|
||||
|
||||
test("Testing AuthBearerModel toJson", () {
|
||||
var authBearerModel = authBearerModel1;
|
||||
expect(authBearerModel.toJson(), authBearerModelJson1);
|
||||
});
|
||||
|
||||
test("Testing AuthBearerModel fromJson", () {
|
||||
var authBearerModel = authBearerModel1;
|
||||
final modelFromJson = AuthBearerModel.fromJson(authBearerModelJson1);
|
||||
expect(modelFromJson, authBearerModel);
|
||||
expect(
|
||||
modelFromJson.token,
|
||||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
|
||||
);
|
||||
});
|
||||
|
||||
test("Testing AuthBearerModel getters", () {
|
||||
var authBearerModel = authBearerModel1;
|
||||
expect(
|
||||
authBearerModel.token,
|
||||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
|
||||
);
|
||||
});
|
||||
|
||||
test("Testing AuthBearerModel equality", () {
|
||||
const authBearerModel1Copy = AuthBearerModel(
|
||||
token:
|
||||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
|
||||
);
|
||||
expect(authBearerModel1, authBearerModel1Copy);
|
||||
expect(authBearerModel1, isNot(authBearerModel2));
|
||||
});
|
||||
|
||||
test("Testing AuthBearerModel with different tokens", () {
|
||||
expect(authBearerModel2.token, 'different_bearer_token_value');
|
||||
expect(authBearerModel1.token, isNot(authBearerModel2.token));
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import 'package:better_networking/models/auth/auth_digest_model.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'auth_models.dart';
|
||||
|
||||
void main() {
|
||||
group('Testing AuthDigestModel', () {
|
||||
test("Testing AuthDigestModel copyWith", () {
|
||||
var authDigestModel = authDigestModel1;
|
||||
final authDigestModelCopyWith = authDigestModel.copyWith(
|
||||
username: 'new_user',
|
||||
algorithm: 'MD5',
|
||||
qop: 'auth',
|
||||
);
|
||||
expect(authDigestModelCopyWith.username, 'new_user');
|
||||
expect(authDigestModelCopyWith.algorithm, 'MD5');
|
||||
expect(authDigestModelCopyWith.qop, 'auth');
|
||||
// original model unchanged
|
||||
expect(authDigestModel.username, 'digest_user');
|
||||
expect(authDigestModel.algorithm, 'SHA-256');
|
||||
expect(authDigestModel.qop, 'auth-int');
|
||||
});
|
||||
|
||||
test("Testing AuthDigestModel toJson", () {
|
||||
var authDigestModel = authDigestModel1;
|
||||
expect(authDigestModel.toJson(), authDigestModelJson1);
|
||||
});
|
||||
|
||||
test("Testing AuthDigestModel fromJson", () {
|
||||
var authDigestModel = authDigestModel1;
|
||||
final modelFromJson = AuthDigestModel.fromJson(authDigestModelJson1);
|
||||
expect(modelFromJson, authDigestModel);
|
||||
expect(modelFromJson.username, 'digest_user');
|
||||
expect(modelFromJson.password, 'digest_pass');
|
||||
expect(modelFromJson.realm, 'protected-area');
|
||||
expect(modelFromJson.algorithm, 'SHA-256');
|
||||
});
|
||||
|
||||
test("Testing AuthDigestModel getters", () {
|
||||
var authDigestModel = authDigestModel1;
|
||||
expect(authDigestModel.username, 'digest_user');
|
||||
expect(authDigestModel.password, 'digest_pass');
|
||||
expect(authDigestModel.realm, 'protected-area');
|
||||
expect(authDigestModel.nonce, 'dcd98b7102dd2f0e8b11d0f600bfb0c093');
|
||||
expect(authDigestModel.algorithm, 'SHA-256');
|
||||
expect(authDigestModel.qop, 'auth-int');
|
||||
expect(authDigestModel.opaque, '5ccc069c403ebaf9f0171e9517f40e41');
|
||||
});
|
||||
|
||||
test("Testing AuthDigestModel equality", () {
|
||||
const authDigestModel1Copy = AuthDigestModel(
|
||||
username: 'digest_user',
|
||||
password: 'digest_pass',
|
||||
realm: 'protected-area',
|
||||
nonce: 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
|
||||
algorithm: 'SHA-256',
|
||||
qop: 'auth-int',
|
||||
opaque: '5ccc069c403ebaf9f0171e9517f40e41',
|
||||
);
|
||||
expect(authDigestModel1, authDigestModel1Copy);
|
||||
expect(authDigestModel1, isNot(authDigestModel2));
|
||||
});
|
||||
|
||||
test("Testing AuthDigestModel with different configurations", () {
|
||||
expect(authDigestModel2.username, 'another_digest_user');
|
||||
expect(authDigestModel2.password, 'another_digest_pass');
|
||||
expect(authDigestModel2.realm, 'different-realm');
|
||||
expect(authDigestModel2.nonce, 'abc12345678901234567890abcdef012');
|
||||
expect(authDigestModel2.algorithm, 'MD5');
|
||||
expect(authDigestModel2.qop, 'auth');
|
||||
expect(authDigestModel2.opaque, 'fedcba0987654321098765432109876543');
|
||||
|
||||
// Compare differences
|
||||
expect(authDigestModel1.username, isNot(authDigestModel2.username));
|
||||
expect(authDigestModel1.algorithm, isNot(authDigestModel2.algorithm));
|
||||
expect(authDigestModel1.qop, isNot(authDigestModel2.qop));
|
||||
expect(authDigestModel1.realm, isNot(authDigestModel2.realm));
|
||||
});
|
||||
|
||||
test("Testing AuthDigestModel nonce and opaque values", () {
|
||||
var authDigestModel = authDigestModel1;
|
||||
expect(authDigestModel.nonce.length, 34);
|
||||
expect(authDigestModel.opaque.length, 32);
|
||||
expect(authDigestModel.nonce, matches(RegExp(r'^[a-f0-9]+$')));
|
||||
expect(authDigestModel.opaque, matches(RegExp(r'^[a-f0-9]+$')));
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import 'package:better_networking/models/auth/auth_jwt_model.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'auth_models.dart';
|
||||
|
||||
void main() {
|
||||
group('Testing AuthJwtModel', () {
|
||||
test("Testing AuthJwtModel copyWith", () {
|
||||
var authJwtModel = authJwtModel1;
|
||||
final authJwtModelCopyWith = authJwtModel.copyWith(
|
||||
secret: 'new_secret',
|
||||
algorithm: 'HS256',
|
||||
isSecretBase64Encoded: false,
|
||||
);
|
||||
expect(authJwtModelCopyWith.secret, 'new_secret');
|
||||
expect(authJwtModelCopyWith.algorithm, 'HS256');
|
||||
expect(authJwtModelCopyWith.isSecretBase64Encoded, false);
|
||||
// original model unchanged
|
||||
expect(authJwtModel.secret, 'jwt-secret-key');
|
||||
expect(authJwtModel.algorithm, 'RS256');
|
||||
expect(authJwtModel.isSecretBase64Encoded, true);
|
||||
});
|
||||
|
||||
test("Testing AuthJwtModel toJson", () {
|
||||
var authJwtModel = authJwtModel1;
|
||||
expect(authJwtModel.toJson(), authJwtModelJson1);
|
||||
});
|
||||
|
||||
test("Testing AuthJwtModel fromJson", () {
|
||||
var authJwtModel = authJwtModel1;
|
||||
final modelFromJson = AuthJwtModel.fromJson(authJwtModelJson1);
|
||||
expect(modelFromJson, authJwtModel);
|
||||
expect(modelFromJson.secret, 'jwt-secret-key');
|
||||
expect(modelFromJson.algorithm, 'RS256');
|
||||
expect(modelFromJson.isSecretBase64Encoded, true);
|
||||
});
|
||||
|
||||
test("Testing AuthJwtModel getters", () {
|
||||
var authJwtModel = authJwtModel1;
|
||||
expect(authJwtModel.secret, 'jwt-secret-key');
|
||||
expect(
|
||||
authJwtModel.privateKey,
|
||||
startsWith('-----BEGIN RSA PRIVATE KEY-----'),
|
||||
);
|
||||
expect(authJwtModel.payload, '{"user_id": 123, "exp": 1735689600}');
|
||||
expect(authJwtModel.addTokenTo, 'header');
|
||||
expect(authJwtModel.algorithm, 'RS256');
|
||||
expect(authJwtModel.isSecretBase64Encoded, true);
|
||||
expect(authJwtModel.headerPrefix, 'JWT');
|
||||
expect(authJwtModel.queryParamKey, 'jwt_token');
|
||||
expect(authJwtModel.header, 'X-JWT-Token');
|
||||
});
|
||||
|
||||
test("Testing AuthJwtModel equality", () {
|
||||
const authJwtModel1Copy = AuthJwtModel(
|
||||
secret: 'jwt-secret-key',
|
||||
privateKey: '''-----BEGIN RSA PRIVATE KEY-----
|
||||
MIICWgIBAAKBgHa+iOFqaom/Eg1xlBapqu6JPDHMhsCLy06i4/yZ6KFTz8RWBDG8
|
||||
rRdhqSTOWCGtLq+unK/A1lkexaYE3lHBbn/2dzDjaXA48G/B4s4R6ixigQDWnZJd
|
||||
e4GVKuLOZx82tDSl0yLQOzOzUMygj8IRBgp7CaL4WBRo5DwGRXAON9A7AgMBAAEC
|
||||
gYAlotZ3u+bwqeLq5+jsFfLbkBvIHO9I8AYMcoyYb5/QImRj8m955Ddohce6prxA
|
||||
UEfP3yRCgHhv3tT+feSJPSnsbPIpWnmnvDdy+NLij6rYKjga8oYyskg8wpYKSsgO
|
||||
nNTWI8jLDTM2TFGXAR+Pn+yQ120fmcdhMKsnshnxitHhAQJBAM58Tz/SKb+Hgojs
|
||||
Le3WJfs1meK0ecEHVZr9p+8mXmn1qUWddG/Mi1m2Zr3ycef+JMDp8CKexa/dacSV
|
||||
00D+G6ECQQCTN/tEBBia1+eMy3+GKYVH/M7jVSPxjcTQF3qnBnd752AJNqHUpaFO
|
||||
af8d1omyRY8DdCgTs/JjfesveaL0Uz5bAkB+bVCctBKJye/b5DhO+qLwyCX70CMI
|
||||
VHRO3Oa5IBYI7LiC/mBvn57nBC6uOMcTk+FvGQ3GNM632mrLSi06CxxhAkA92BeS
|
||||
xBG+ApL//4DL0GdwDVCwCVU3JTIXpLVeswXApDsgw7WKCiZQNZD5bOWdYUEp10L6
|
||||
u+5IQ15oLDX7Y3jfAkBtpWyAuhQwYLpiLPa82U9zus9IxYpfqohVBeiT5UasSssx
|
||||
OUdMDWRxHzjEexN0nmD1nIPbKNJd0/rvj7jI82Kh
|
||||
-----END RSA PRIVATE KEY-----''',
|
||||
payload: '{"user_id": 123, "exp": 1735689600}',
|
||||
addTokenTo: 'header',
|
||||
algorithm: 'RS256',
|
||||
isSecretBase64Encoded: true,
|
||||
headerPrefix: 'JWT',
|
||||
queryParamKey: 'jwt_token',
|
||||
header: 'X-JWT-Token',
|
||||
);
|
||||
expect(authJwtModel1, authJwtModel1Copy);
|
||||
expect(authJwtModel1, isNot(authJwtModel2));
|
||||
});
|
||||
|
||||
test("Testing AuthJwtModel with different configurations", () {
|
||||
expect(authJwtModel2.secret, 'different-jwt-secret');
|
||||
expect(authJwtModel2.algorithm, 'HS256');
|
||||
expect(authJwtModel2.isSecretBase64Encoded, false);
|
||||
expect(authJwtModel2.addTokenTo, 'query');
|
||||
expect(authJwtModel2.headerPrefix, 'Bearer');
|
||||
expect(authJwtModel2.header, 'Authorization');
|
||||
|
||||
// Compare differences
|
||||
expect(authJwtModel1.secret, isNot(authJwtModel2.secret));
|
||||
expect(authJwtModel1.algorithm, isNot(authJwtModel2.algorithm));
|
||||
expect(
|
||||
authJwtModel1.isSecretBase64Encoded,
|
||||
isNot(authJwtModel2.isSecretBase64Encoded),
|
||||
);
|
||||
expect(authJwtModel1.addTokenTo, isNot(authJwtModel2.addTokenTo));
|
||||
});
|
||||
|
||||
test("Testing AuthJwtModel payload parsing", () {
|
||||
var authJwtModel = authJwtModel1;
|
||||
expect(authJwtModel.payload, contains('user_id'));
|
||||
expect(authJwtModel.payload, contains('exp'));
|
||||
|
||||
var authJwtModel2Local = authJwtModel2;
|
||||
expect(authJwtModel2Local.payload, contains('sub'));
|
||||
expect(authJwtModel2Local.payload, contains('name'));
|
||||
expect(authJwtModel2Local.payload, contains('John Doe'));
|
||||
});
|
||||
});
|
||||
}
|
||||
212
packages/better_networking/test/models/auth/auth_models.dart
Normal file
212
packages/better_networking/test/models/auth/auth_models.dart
Normal file
@@ -0,0 +1,212 @@
|
||||
import 'package:better_networking/models/auth/api_auth_model.dart';
|
||||
import 'package:better_networking/models/auth/auth_basic_model.dart';
|
||||
import 'package:better_networking/models/auth/auth_bearer_model.dart';
|
||||
import 'package:better_networking/models/auth/auth_api_key_model.dart';
|
||||
import 'package:better_networking/models/auth/auth_jwt_model.dart';
|
||||
import 'package:better_networking/models/auth/auth_digest_model.dart';
|
||||
import 'package:better_networking/consts.dart';
|
||||
|
||||
/// Auth models for testing
|
||||
|
||||
const authBasicModel1 = AuthBasicAuthModel(
|
||||
username: 'john_doe',
|
||||
password: 'secure_password',
|
||||
);
|
||||
|
||||
const authBasicModel2 = AuthBasicAuthModel(
|
||||
username: 'jane_smith',
|
||||
password: 'another_password',
|
||||
);
|
||||
|
||||
const authBearerModel1 = AuthBearerModel(
|
||||
token:
|
||||
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
|
||||
);
|
||||
|
||||
const authBearerModel2 = AuthBearerModel(token: 'different_bearer_token_value');
|
||||
|
||||
const authApiKeyModel1 = AuthApiKeyModel(
|
||||
key: 'ak-test-key-12345',
|
||||
location: 'header',
|
||||
name: 'x-api-key',
|
||||
);
|
||||
|
||||
const authApiKeyModel2 = AuthApiKeyModel(
|
||||
key: 'query-api-key-67890',
|
||||
location: 'query',
|
||||
name: 'api_key',
|
||||
);
|
||||
|
||||
const authJwtModel1 = AuthJwtModel(
|
||||
secret: 'jwt-secret-key',
|
||||
privateKey: '''-----BEGIN RSA PRIVATE KEY-----
|
||||
MIICWgIBAAKBgHa+iOFqaom/Eg1xlBapqu6JPDHMhsCLy06i4/yZ6KFTz8RWBDG8
|
||||
rRdhqSTOWCGtLq+unK/A1lkexaYE3lHBbn/2dzDjaXA48G/B4s4R6ixigQDWnZJd
|
||||
e4GVKuLOZx82tDSl0yLQOzOzUMygj8IRBgp7CaL4WBRo5DwGRXAON9A7AgMBAAEC
|
||||
gYAlotZ3u+bwqeLq5+jsFfLbkBvIHO9I8AYMcoyYb5/QImRj8m955Ddohce6prxA
|
||||
UEfP3yRCgHhv3tT+feSJPSnsbPIpWnmnvDdy+NLij6rYKjga8oYyskg8wpYKSsgO
|
||||
nNTWI8jLDTM2TFGXAR+Pn+yQ120fmcdhMKsnshnxitHhAQJBAM58Tz/SKb+Hgojs
|
||||
Le3WJfs1meK0ecEHVZr9p+8mXmn1qUWddG/Mi1m2Zr3ycef+JMDp8CKexa/dacSV
|
||||
00D+G6ECQQCTN/tEBBia1+eMy3+GKYVH/M7jVSPxjcTQF3qnBnd752AJNqHUpaFO
|
||||
af8d1omyRY8DdCgTs/JjfesveaL0Uz5bAkB+bVCctBKJye/b5DhO+qLwyCX70CMI
|
||||
VHRO3Oa5IBYI7LiC/mBvn57nBC6uOMcTk+FvGQ3GNM632mrLSi06CxxhAkA92BeS
|
||||
xBG+ApL//4DL0GdwDVCwCVU3JTIXpLVeswXApDsgw7WKCiZQNZD5bOWdYUEp10L6
|
||||
u+5IQ15oLDX7Y3jfAkBtpWyAuhQwYLpiLPa82U9zus9IxYpfqohVBeiT5UasSssx
|
||||
OUdMDWRxHzjEexN0nmD1nIPbKNJd0/rvj7jI82Kh
|
||||
-----END RSA PRIVATE KEY-----''',
|
||||
payload: '{"user_id": 123, "exp": 1735689600}',
|
||||
addTokenTo: 'header',
|
||||
algorithm: 'RS256',
|
||||
isSecretBase64Encoded: true,
|
||||
headerPrefix: 'JWT',
|
||||
queryParamKey: 'jwt_token',
|
||||
header: 'X-JWT-Token',
|
||||
);
|
||||
|
||||
const authJwtModel2 = AuthJwtModel(
|
||||
secret: 'different-jwt-secret',
|
||||
payload: '{"sub": "1234567890", "name": "John Doe", "iat": 1516239022}',
|
||||
addTokenTo: 'query',
|
||||
algorithm: 'HS256',
|
||||
isSecretBase64Encoded: false,
|
||||
headerPrefix: 'Bearer',
|
||||
queryParamKey: 'token',
|
||||
header: 'Authorization',
|
||||
);
|
||||
|
||||
const authDigestModel1 = AuthDigestModel(
|
||||
username: 'digest_user',
|
||||
password: 'digest_pass',
|
||||
realm: 'protected-area',
|
||||
nonce: 'dcd98b7102dd2f0e8b11d0f600bfb0c093',
|
||||
algorithm: 'SHA-256',
|
||||
qop: 'auth-int',
|
||||
opaque: '5ccc069c403ebaf9f0171e9517f40e41',
|
||||
);
|
||||
|
||||
const authDigestModel2 = AuthDigestModel(
|
||||
username: 'another_digest_user',
|
||||
password: 'another_digest_pass',
|
||||
realm: 'different-realm',
|
||||
nonce: 'abc12345678901234567890abcdef012',
|
||||
algorithm: 'MD5',
|
||||
qop: 'auth',
|
||||
opaque: 'fedcba0987654321098765432109876543',
|
||||
);
|
||||
|
||||
const authModel1 = AuthModel(type: APIAuthType.basic, basic: authBasicModel1);
|
||||
|
||||
const authModel2 = AuthModel(
|
||||
type: APIAuthType.bearer,
|
||||
bearer: authBearerModel1,
|
||||
);
|
||||
|
||||
const authModel3 = AuthModel(
|
||||
type: APIAuthType.apiKey,
|
||||
apikey: authApiKeyModel1,
|
||||
);
|
||||
|
||||
const authModel4 = AuthModel(type: APIAuthType.jwt, jwt: authJwtModel1);
|
||||
|
||||
const authModel5 = AuthModel(
|
||||
type: APIAuthType.digest,
|
||||
digest: authDigestModel1,
|
||||
);
|
||||
|
||||
const authModelNone = AuthModel(type: APIAuthType.none);
|
||||
|
||||
/// JSON representations for testing
|
||||
|
||||
final Map<String, dynamic> authBasicModelJson1 = {
|
||||
"username": "john_doe",
|
||||
"password": "secure_password",
|
||||
};
|
||||
|
||||
final Map<String, dynamic> authBearerModelJson1 = {
|
||||
"token":
|
||||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
|
||||
};
|
||||
|
||||
final Map<String, dynamic> authApiKeyModelJson1 = {
|
||||
"key": "ak-test-key-12345",
|
||||
"location": "header",
|
||||
"name": "x-api-key",
|
||||
};
|
||||
|
||||
final Map<String, dynamic> authJwtModelJson1 = {
|
||||
"secret": "jwt-secret-key",
|
||||
"privateKey": '''-----BEGIN RSA PRIVATE KEY-----
|
||||
MIICWgIBAAKBgHa+iOFqaom/Eg1xlBapqu6JPDHMhsCLy06i4/yZ6KFTz8RWBDG8
|
||||
rRdhqSTOWCGtLq+unK/A1lkexaYE3lHBbn/2dzDjaXA48G/B4s4R6ixigQDWnZJd
|
||||
e4GVKuLOZx82tDSl0yLQOzOzUMygj8IRBgp7CaL4WBRo5DwGRXAON9A7AgMBAAEC
|
||||
gYAlotZ3u+bwqeLq5+jsFfLbkBvIHO9I8AYMcoyYb5/QImRj8m955Ddohce6prxA
|
||||
UEfP3yRCgHhv3tT+feSJPSnsbPIpWnmnvDdy+NLij6rYKjga8oYyskg8wpYKSsgO
|
||||
nNTWI8jLDTM2TFGXAR+Pn+yQ120fmcdhMKsnshnxitHhAQJBAM58Tz/SKb+Hgojs
|
||||
Le3WJfs1meK0ecEHVZr9p+8mXmn1qUWddG/Mi1m2Zr3ycef+JMDp8CKexa/dacSV
|
||||
00D+G6ECQQCTN/tEBBia1+eMy3+GKYVH/M7jVSPxjcTQF3qnBnd752AJNqHUpaFO
|
||||
af8d1omyRY8DdCgTs/JjfesveaL0Uz5bAkB+bVCctBKJye/b5DhO+qLwyCX70CMI
|
||||
VHRO3Oa5IBYI7LiC/mBvn57nBC6uOMcTk+FvGQ3GNM632mrLSi06CxxhAkA92BeS
|
||||
xBG+ApL//4DL0GdwDVCwCVU3JTIXpLVeswXApDsgw7WKCiZQNZD5bOWdYUEp10L6
|
||||
u+5IQ15oLDX7Y3jfAkBtpWyAuhQwYLpiLPa82U9zus9IxYpfqohVBeiT5UasSssx
|
||||
OUdMDWRxHzjEexN0nmD1nIPbKNJd0/rvj7jI82Kh
|
||||
-----END RSA PRIVATE KEY-----''',
|
||||
"payload": "{\"user_id\": 123, \"exp\": 1735689600}",
|
||||
"addTokenTo": "header",
|
||||
"algorithm": "RS256",
|
||||
"isSecretBase64Encoded": true,
|
||||
"headerPrefix": "JWT",
|
||||
"queryParamKey": "jwt_token",
|
||||
"header": "X-JWT-Token",
|
||||
};
|
||||
|
||||
final Map<String, dynamic> authDigestModelJson1 = {
|
||||
"username": "digest_user",
|
||||
"password": "digest_pass",
|
||||
"realm": "protected-area",
|
||||
"nonce": "dcd98b7102dd2f0e8b11d0f600bfb0c093",
|
||||
"algorithm": "SHA-256",
|
||||
"qop": "auth-int",
|
||||
"opaque": "5ccc069c403ebaf9f0171e9517f40e41",
|
||||
};
|
||||
|
||||
final Map<String, dynamic> authModelJson1 = {
|
||||
"type": "basic",
|
||||
"apikey": null,
|
||||
"bearer": null,
|
||||
"basic": {"username": "john_doe", "password": "secure_password"},
|
||||
"jwt": null,
|
||||
"digest": null,
|
||||
};
|
||||
|
||||
final Map<String, dynamic> authModelJson2 = {
|
||||
"type": "bearer",
|
||||
"apikey": null,
|
||||
"bearer": {
|
||||
"token":
|
||||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
|
||||
},
|
||||
"basic": null,
|
||||
"jwt": null,
|
||||
"digest": null,
|
||||
};
|
||||
|
||||
final Map<String, dynamic> authModelJson3 = {
|
||||
"type": "",
|
||||
"apikey": null,
|
||||
"bearer": {
|
||||
"token":
|
||||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
|
||||
},
|
||||
"basic": null,
|
||||
"jwt": null,
|
||||
"digest": null,
|
||||
};
|
||||
|
||||
final Map<String, dynamic> authModelNoneJson = {
|
||||
"type": "none",
|
||||
"apikey": null,
|
||||
"bearer": null,
|
||||
"basic": null,
|
||||
"jwt": null,
|
||||
"digest": null,
|
||||
};
|
||||
@@ -51,7 +51,7 @@ void main() {
|
||||
|
||||
final result = await handleAuth(httpRequestModel, authModel);
|
||||
|
||||
expect(result.headers, isEmpty);
|
||||
expect(result.headers, isNull);
|
||||
expect(result.url, equals('https://api.apidash.dev/users'));
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user