539 lines
17 KiB
Dart
539 lines
17 KiB
Dart
import 'package:swagger_generator_flutter/core/models.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
void main() {
|
|
group('OAuth2 Flow', () {
|
|
test('creates OAuth2Flow with all fields', () {
|
|
final json = {
|
|
'authorizationUrl': 'https://example.com/oauth/authorize',
|
|
'tokenUrl': 'https://example.com/oauth/token',
|
|
'refreshUrl': 'https://example.com/oauth/refresh',
|
|
'scopes': {
|
|
'read': 'Read access',
|
|
'write': 'Write access',
|
|
'admin': 'Admin access',
|
|
},
|
|
};
|
|
|
|
final flow = OAuth2Flow.fromJson(json);
|
|
|
|
expect(flow.authorizationUrl, 'https://example.com/oauth/authorize');
|
|
expect(flow.tokenUrl, 'https://example.com/oauth/token');
|
|
expect(flow.refreshUrl, 'https://example.com/oauth/refresh');
|
|
expect(flow.scopes.length, 3);
|
|
expect(flow.scopes['read'], 'Read access');
|
|
expect(flow.scopes['write'], 'Write access');
|
|
expect(flow.scopes['admin'], 'Admin access');
|
|
expect(flow.hasAuthorizationUrl, true);
|
|
expect(flow.hasTokenUrl, true);
|
|
expect(flow.hasRefreshUrl, true);
|
|
expect(flow.hasScopes, true);
|
|
});
|
|
|
|
test('creates OAuth2Flow with minimal fields', () {
|
|
final json = {
|
|
'tokenUrl': 'https://example.com/oauth/token',
|
|
'scopes': {},
|
|
};
|
|
|
|
final flow = OAuth2Flow.fromJson(json);
|
|
|
|
expect(flow.authorizationUrl, isNull);
|
|
expect(flow.tokenUrl, 'https://example.com/oauth/token');
|
|
expect(flow.refreshUrl, isNull);
|
|
expect(flow.scopes, isEmpty);
|
|
expect(flow.hasAuthorizationUrl, false);
|
|
expect(flow.hasTokenUrl, true);
|
|
expect(flow.hasRefreshUrl, false);
|
|
expect(flow.hasScopes, false);
|
|
});
|
|
});
|
|
|
|
group('OAuth2 Flows', () {
|
|
test('creates OAuth2Flows with multiple flow types', () {
|
|
final json = {
|
|
'authorizationCode': {
|
|
'authorizationUrl': 'https://example.com/oauth/authorize',
|
|
'tokenUrl': 'https://example.com/oauth/token',
|
|
'scopes': {
|
|
'read': 'Read access',
|
|
'write': 'Write access',
|
|
},
|
|
},
|
|
'implicit': {
|
|
'authorizationUrl': 'https://example.com/oauth/authorize',
|
|
'scopes': {
|
|
'read': 'Read access',
|
|
},
|
|
},
|
|
'clientCredentials': {
|
|
'tokenUrl': 'https://example.com/oauth/token',
|
|
'scopes': {
|
|
'admin': 'Admin access',
|
|
},
|
|
},
|
|
};
|
|
|
|
final flows = OAuth2Flows.fromJson(json);
|
|
|
|
expect(flows.hasAnyFlow, true);
|
|
expect(flows.availableFlows.length, 3);
|
|
expect(
|
|
flows.availableFlows.contains(OAuth2FlowType.authorizationCode),
|
|
true,
|
|
);
|
|
expect(flows.availableFlows.contains(OAuth2FlowType.implicit), true);
|
|
expect(
|
|
flows.availableFlows.contains(OAuth2FlowType.clientCredentials),
|
|
true,
|
|
);
|
|
expect(flows.availableFlows.contains(OAuth2FlowType.password), false);
|
|
|
|
expect(flows.authorizationCode, isNotNull);
|
|
expect(flows.implicit, isNotNull);
|
|
expect(flows.password, isNull);
|
|
expect(flows.clientCredentials, isNotNull);
|
|
|
|
expect(flows.getFlow(OAuth2FlowType.authorizationCode), isNotNull);
|
|
expect(flows.getFlow(OAuth2FlowType.password), isNull);
|
|
});
|
|
|
|
test('creates empty OAuth2Flows', () {
|
|
final json = <String, dynamic>{};
|
|
|
|
final flows = OAuth2Flows.fromJson(json);
|
|
|
|
expect(flows.hasAnyFlow, false);
|
|
expect(flows.availableFlows, isEmpty);
|
|
expect(flows.authorizationCode, isNull);
|
|
expect(flows.implicit, isNull);
|
|
expect(flows.password, isNull);
|
|
expect(flows.clientCredentials, isNull);
|
|
});
|
|
});
|
|
|
|
group('ApiSecurityScheme', () {
|
|
test('creates API Key security scheme', () {
|
|
final json = {
|
|
'type': 'apiKey',
|
|
'description': 'API Key authentication',
|
|
'name': 'X-API-Key',
|
|
'in': 'header',
|
|
};
|
|
|
|
final scheme = ApiSecurityScheme.fromJson(json);
|
|
|
|
expect(scheme.type, SecuritySchemeType.apiKey);
|
|
expect(scheme.description, 'API Key authentication');
|
|
expect(scheme.name, 'X-API-Key');
|
|
expect(scheme.location, ApiKeyLocation.header);
|
|
expect(scheme.isApiKey, true);
|
|
expect(scheme.isHttp, false);
|
|
expect(scheme.isOAuth2, false);
|
|
expect(scheme.isOpenIdConnect, false);
|
|
expect(scheme.apiKeyInfo, 'header:X-API-Key');
|
|
});
|
|
|
|
test('creates HTTP Bearer security scheme', () {
|
|
final json = {
|
|
'type': 'http',
|
|
'description': 'Bearer token authentication',
|
|
'scheme': 'bearer',
|
|
'bearerFormat': 'JWT',
|
|
};
|
|
|
|
final scheme = ApiSecurityScheme.fromJson(json);
|
|
|
|
expect(scheme.type, SecuritySchemeType.http);
|
|
expect(scheme.description, 'Bearer token authentication');
|
|
expect(scheme.scheme, 'bearer');
|
|
expect(scheme.bearerFormat, 'JWT');
|
|
expect(scheme.isHttp, true);
|
|
expect(scheme.isBearer, true);
|
|
expect(scheme.isBasic, false);
|
|
expect(scheme.httpAuthInfo, 'bearer (JWT)');
|
|
});
|
|
|
|
test('creates HTTP Basic security scheme', () {
|
|
final json = {
|
|
'type': 'http',
|
|
'description': 'Basic authentication',
|
|
'scheme': 'basic',
|
|
};
|
|
|
|
final scheme = ApiSecurityScheme.fromJson(json);
|
|
|
|
expect(scheme.type, SecuritySchemeType.http);
|
|
expect(scheme.scheme, 'basic');
|
|
expect(scheme.isHttp, true);
|
|
expect(scheme.isBasic, true);
|
|
expect(scheme.isBearer, false);
|
|
expect(scheme.httpAuthInfo, 'basic');
|
|
});
|
|
|
|
test('creates OAuth2 security scheme', () {
|
|
final json = {
|
|
'type': 'oauth2',
|
|
'description': 'OAuth2 authentication',
|
|
'flows': {
|
|
'authorizationCode': {
|
|
'authorizationUrl': 'https://example.com/oauth/authorize',
|
|
'tokenUrl': 'https://example.com/oauth/token',
|
|
'scopes': {
|
|
'read': 'Read access',
|
|
'write': 'Write access',
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
final scheme = ApiSecurityScheme.fromJson(json);
|
|
|
|
expect(scheme.type, SecuritySchemeType.oauth2);
|
|
expect(scheme.description, 'OAuth2 authentication');
|
|
expect(scheme.isOAuth2, true);
|
|
expect(scheme.hasOAuth2Flows, true);
|
|
expect(scheme.flows?.hasAnyFlow, true);
|
|
expect(scheme.flows?.authorizationCode, isNotNull);
|
|
});
|
|
|
|
test('creates OpenID Connect security scheme', () {
|
|
final json = {
|
|
'type': 'openIdConnect',
|
|
'description': 'OpenID Connect authentication',
|
|
'openIdConnectUrl':
|
|
'https://example.com/.well-known/openid_configuration',
|
|
};
|
|
|
|
final scheme = ApiSecurityScheme.fromJson(json);
|
|
|
|
expect(scheme.type, SecuritySchemeType.openIdConnect);
|
|
expect(scheme.description, 'OpenID Connect authentication');
|
|
expect(
|
|
scheme.openIdConnectUrl,
|
|
'https://example.com/.well-known/openid_configuration',
|
|
);
|
|
expect(scheme.isOpenIdConnect, true);
|
|
});
|
|
});
|
|
|
|
group('ApiSecurityRequirement', () {
|
|
test('creates security requirement with scopes', () {
|
|
final json = {
|
|
'oauth2': ['read', 'write'],
|
|
'apiKey': [],
|
|
};
|
|
|
|
final requirement = ApiSecurityRequirement.fromJson(json);
|
|
|
|
expect(requirement.isNotEmpty, true);
|
|
expect(requirement.schemeNames.length, 2);
|
|
expect(requirement.hasScheme('oauth2'), true);
|
|
expect(requirement.hasScheme('apiKey'), true);
|
|
expect(requirement.hasScheme('unknown'), false);
|
|
expect(requirement.getScopesForScheme('oauth2'), ['read', 'write']);
|
|
expect(requirement.getScopesForScheme('apiKey'), isEmpty);
|
|
expect(requirement.getScopesForScheme('unknown'), isEmpty);
|
|
});
|
|
|
|
test('creates empty security requirement', () {
|
|
final json = <String, dynamic>{};
|
|
|
|
final requirement = ApiSecurityRequirement.fromJson(json);
|
|
|
|
expect(requirement.isEmpty, true);
|
|
expect(requirement.isNotEmpty, false);
|
|
expect(requirement.schemeNames, isEmpty);
|
|
});
|
|
});
|
|
|
|
group('Security Scheme Types', () {
|
|
test('converts security scheme type to string', () {
|
|
expect(SecuritySchemeType.apiKey.value, 'apiKey');
|
|
expect(SecuritySchemeType.http.value, 'http');
|
|
expect(SecuritySchemeType.oauth2.value, 'oauth2');
|
|
expect(SecuritySchemeType.openIdConnect.value, 'openIdConnect');
|
|
});
|
|
|
|
test('converts string to security scheme type', () {
|
|
expect(
|
|
SecuritySchemeTypeExtension.fromString('apiKey'),
|
|
SecuritySchemeType.apiKey,
|
|
);
|
|
expect(
|
|
SecuritySchemeTypeExtension.fromString('http'),
|
|
SecuritySchemeType.http,
|
|
);
|
|
expect(
|
|
SecuritySchemeTypeExtension.fromString('oauth2'),
|
|
SecuritySchemeType.oauth2,
|
|
);
|
|
expect(
|
|
SecuritySchemeTypeExtension.fromString('openIdConnect'),
|
|
SecuritySchemeType.openIdConnect,
|
|
);
|
|
expect(
|
|
SecuritySchemeTypeExtension.fromString('unknown'),
|
|
SecuritySchemeType.apiKey,
|
|
);
|
|
});
|
|
|
|
test('converts API key location to string', () {
|
|
expect(ApiKeyLocation.query.value, 'query');
|
|
expect(ApiKeyLocation.header.value, 'header');
|
|
expect(ApiKeyLocation.cookie.value, 'cookie');
|
|
});
|
|
|
|
test('converts string to API key location', () {
|
|
expect(ApiKeyLocationExtension.fromString('query'), ApiKeyLocation.query);
|
|
expect(
|
|
ApiKeyLocationExtension.fromString('header'),
|
|
ApiKeyLocation.header,
|
|
);
|
|
expect(
|
|
ApiKeyLocationExtension.fromString('cookie'),
|
|
ApiKeyLocation.cookie,
|
|
);
|
|
expect(
|
|
ApiKeyLocationExtension.fromString('unknown'),
|
|
ApiKeyLocation.header,
|
|
);
|
|
});
|
|
|
|
test('converts OAuth2 flow type to string', () {
|
|
expect(OAuth2FlowType.authorizationCode.value, 'authorizationCode');
|
|
expect(OAuth2FlowType.implicit.value, 'implicit');
|
|
expect(OAuth2FlowType.password.value, 'password');
|
|
expect(OAuth2FlowType.clientCredentials.value, 'clientCredentials');
|
|
});
|
|
|
|
test('converts string to OAuth2 flow type', () {
|
|
expect(
|
|
OAuth2FlowTypeExtension.fromString('authorizationCode'),
|
|
OAuth2FlowType.authorizationCode,
|
|
);
|
|
expect(
|
|
OAuth2FlowTypeExtension.fromString('implicit'),
|
|
OAuth2FlowType.implicit,
|
|
);
|
|
expect(
|
|
OAuth2FlowTypeExtension.fromString('password'),
|
|
OAuth2FlowType.password,
|
|
);
|
|
expect(
|
|
OAuth2FlowTypeExtension.fromString('clientCredentials'),
|
|
OAuth2FlowType.clientCredentials,
|
|
);
|
|
expect(
|
|
OAuth2FlowTypeExtension.fromString('unknown'),
|
|
OAuth2FlowType.authorizationCode,
|
|
);
|
|
});
|
|
});
|
|
|
|
group('API Key Authentication Integration', () {
|
|
test('creates complete API Key security scheme for header', () {
|
|
final json = {
|
|
'type': 'apiKey',
|
|
'description': 'API Key in header',
|
|
'name': 'X-API-Key',
|
|
'in': 'header',
|
|
};
|
|
|
|
final scheme = ApiSecurityScheme.fromJson(json);
|
|
|
|
expect(scheme.isApiKey, true);
|
|
expect(scheme.name, 'X-API-Key');
|
|
expect(scheme.location, ApiKeyLocation.header);
|
|
expect(scheme.apiKeyInfo, 'header:X-API-Key');
|
|
});
|
|
|
|
test('creates complete API Key security scheme for query', () {
|
|
final json = {
|
|
'type': 'apiKey',
|
|
'description': 'API Key in query parameter',
|
|
'name': 'api_key',
|
|
'in': 'query',
|
|
};
|
|
|
|
final scheme = ApiSecurityScheme.fromJson(json);
|
|
|
|
expect(scheme.isApiKey, true);
|
|
expect(scheme.name, 'api_key');
|
|
expect(scheme.location, ApiKeyLocation.query);
|
|
expect(scheme.apiKeyInfo, 'query:api_key');
|
|
});
|
|
|
|
test('creates complete API Key security scheme for cookie', () {
|
|
final json = {
|
|
'type': 'apiKey',
|
|
'description': 'API Key in cookie',
|
|
'name': 'session_token',
|
|
'in': 'cookie',
|
|
};
|
|
|
|
final scheme = ApiSecurityScheme.fromJson(json);
|
|
|
|
expect(scheme.isApiKey, true);
|
|
expect(scheme.name, 'session_token');
|
|
expect(scheme.location, ApiKeyLocation.cookie);
|
|
expect(scheme.apiKeyInfo, 'cookie:session_token');
|
|
});
|
|
|
|
test('handles API Key security requirement', () {
|
|
final requirementJson = {
|
|
'api_key': [],
|
|
};
|
|
|
|
final requirement = ApiSecurityRequirement.fromJson(requirementJson);
|
|
|
|
expect(requirement.hasScheme('api_key'), true);
|
|
expect(requirement.getScopesForScheme('api_key'), isEmpty);
|
|
});
|
|
});
|
|
|
|
group('Bearer Token Authentication Integration', () {
|
|
test('creates complete Bearer token security scheme', () {
|
|
final json = {
|
|
'type': 'http',
|
|
'description': 'Bearer token authentication',
|
|
'scheme': 'bearer',
|
|
'bearerFormat': 'JWT',
|
|
};
|
|
|
|
final scheme = ApiSecurityScheme.fromJson(json);
|
|
|
|
expect(scheme.isHttp, true);
|
|
expect(scheme.isBearer, true);
|
|
expect(scheme.scheme, 'bearer');
|
|
expect(scheme.bearerFormat, 'JWT');
|
|
expect(scheme.httpAuthInfo, 'bearer (JWT)');
|
|
});
|
|
|
|
test('creates Bearer token without format', () {
|
|
final json = {
|
|
'type': 'http',
|
|
'description': 'Bearer token authentication',
|
|
'scheme': 'bearer',
|
|
};
|
|
|
|
final scheme = ApiSecurityScheme.fromJson(json);
|
|
|
|
expect(scheme.isHttp, true);
|
|
expect(scheme.isBearer, true);
|
|
expect(scheme.scheme, 'bearer');
|
|
expect(scheme.bearerFormat, isNull);
|
|
expect(scheme.httpAuthInfo, 'bearer');
|
|
});
|
|
|
|
test('handles Bearer token security requirement', () {
|
|
final requirementJson = {
|
|
'bearerAuth': [],
|
|
};
|
|
|
|
final requirement = ApiSecurityRequirement.fromJson(requirementJson);
|
|
|
|
expect(requirement.hasScheme('bearerAuth'), true);
|
|
expect(requirement.getScopesForScheme('bearerAuth'), isEmpty);
|
|
});
|
|
});
|
|
|
|
group('HTTP Authentication Schemes', () {
|
|
test('creates Basic authentication security scheme', () {
|
|
final json = {
|
|
'type': 'http',
|
|
'description': 'Basic authentication',
|
|
'scheme': 'basic',
|
|
};
|
|
|
|
final scheme = ApiSecurityScheme.fromJson(json);
|
|
|
|
expect(scheme.type, SecuritySchemeType.http);
|
|
expect(scheme.isHttp, true);
|
|
expect(scheme.isBasic, true);
|
|
expect(scheme.isBearer, false);
|
|
expect(scheme.scheme, 'basic');
|
|
expect(scheme.httpAuthInfo, 'basic');
|
|
});
|
|
|
|
test('creates Digest authentication security scheme', () {
|
|
final json = {
|
|
'type': 'http',
|
|
'description': 'Digest authentication',
|
|
'scheme': 'digest',
|
|
};
|
|
|
|
final scheme = ApiSecurityScheme.fromJson(json);
|
|
|
|
expect(scheme.type, SecuritySchemeType.http);
|
|
expect(scheme.isHttp, true);
|
|
expect(scheme.isBasic, false);
|
|
expect(scheme.isBearer, false);
|
|
expect(scheme.scheme, 'digest');
|
|
expect(scheme.httpAuthInfo, 'digest');
|
|
});
|
|
|
|
test('creates custom HTTP authentication security scheme', () {
|
|
final json = {
|
|
'type': 'http',
|
|
'description': 'Custom HTTP authentication',
|
|
'scheme': 'custom',
|
|
};
|
|
|
|
final scheme = ApiSecurityScheme.fromJson(json);
|
|
|
|
expect(scheme.type, SecuritySchemeType.http);
|
|
expect(scheme.isHttp, true);
|
|
expect(scheme.isBasic, false);
|
|
expect(scheme.isBearer, false);
|
|
expect(scheme.scheme, 'custom');
|
|
expect(scheme.httpAuthInfo, 'custom');
|
|
});
|
|
|
|
test('handles HTTP authentication security requirement', () {
|
|
final requirementJson = {
|
|
'basicAuth': [],
|
|
'digestAuth': [],
|
|
};
|
|
|
|
final requirement = ApiSecurityRequirement.fromJson(requirementJson);
|
|
|
|
expect(requirement.hasScheme('basicAuth'), true);
|
|
expect(requirement.hasScheme('digestAuth'), true);
|
|
expect(requirement.getScopesForScheme('basicAuth'), isEmpty);
|
|
expect(requirement.getScopesForScheme('digestAuth'), isEmpty);
|
|
});
|
|
});
|
|
|
|
group('Mixed Security Requirements', () {
|
|
test('handles multiple security schemes in one requirement', () {
|
|
final requirementJson = {
|
|
'oauth2': ['read', 'write'],
|
|
'apiKey': [],
|
|
'basicAuth': [],
|
|
};
|
|
|
|
final requirement = ApiSecurityRequirement.fromJson(requirementJson);
|
|
|
|
expect(requirement.schemeNames.length, 3);
|
|
expect(requirement.hasScheme('oauth2'), true);
|
|
expect(requirement.hasScheme('apiKey'), true);
|
|
expect(requirement.hasScheme('basicAuth'), true);
|
|
expect(requirement.getScopesForScheme('oauth2'), ['read', 'write']);
|
|
expect(requirement.getScopesForScheme('apiKey'), isEmpty);
|
|
expect(requirement.getScopesForScheme('basicAuth'), isEmpty);
|
|
});
|
|
|
|
test('handles empty security requirement (no authentication)', () {
|
|
final requirementJson = <String, dynamic>{};
|
|
|
|
final requirement = ApiSecurityRequirement.fromJson(requirementJson);
|
|
|
|
expect(requirement.isEmpty, true);
|
|
expect(requirement.schemeNames, isEmpty);
|
|
});
|
|
});
|
|
}
|
|
// 忽略测试构造数据的类型推断告警,便于保持示例简洁
|
|
// ignore_for_file: inference_failure_on_collection_literal
|