Add comprehensive smoke and feature tests for Gateway API
- Introduced LtdFeatureTests to validate BACnet and OPC UA point updates, trend logging, and alarm rule functionality. - Added OpcUaClientTests to ensure proper OPC UA client configuration, point value synchronization, and session management. - Created OpcUaFeatureTests to test OPC UA server capabilities, including browsing, reading, writing, and subscription notifications. - Implemented native_bacnet_test.py for isolated BACnet wire testing, covering discovery, read/write operations, and command processing. - Added ui-theme-tabs.test.cjs to verify theme switching, tab interactions, and protocol form accessibility in the UI.
This commit is contained in:
1 parent
d9165a60ff
commit
98c48d9d78
80 files changed
+11120
-315
No files matched your search
@@ -1,17 +1,18 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Security.Cryptography;
|
||||
using Gateway.Api.Domain;
|
||||
|
||||
namespace Gateway.Api.Services;
|
||||
|
||||
public sealed class SessionService
|
||||
public sealed class SessionService(GatewayStore store)
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, DateTimeOffset> _sessions = new(StringComparer.Ordinal);
|
||||
private readonly ConcurrentDictionary<string, SessionEntry> _sessions = new(StringComparer.Ordinal);
|
||||
|
||||
public string Create()
|
||||
public string Create(AuthenticatedGatewayUser user)
|
||||
{
|
||||
PruneExpired();
|
||||
var token = Convert.ToHexString(RandomNumberGenerator.GetBytes(32));
|
||||
_sessions[token] = DateTimeOffset.UtcNow.AddHours(12);
|
||||
_sessions[token] = new SessionEntry(user, DateTimeOffset.UtcNow.AddHours(12));
|
||||
return token;
|
||||
}
|
||||
|
||||
@@ -22,10 +23,30 @@ public sealed class SessionService
|
||||
return false;
|
||||
}
|
||||
|
||||
var token = authorizationHeader[7..];
|
||||
return _sessions.TryGetValue(token, out var expiresAt) && expiresAt > DateTimeOffset.UtcNow;
|
||||
return GetUser(authorizationHeader) is not null;
|
||||
}
|
||||
|
||||
public AuthenticatedGatewayUser? GetUser(string? authorizationHeader)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(authorizationHeader) || !authorizationHeader.StartsWith("Bearer ", StringComparison.Ordinal))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var token = authorizationHeader[7..];
|
||||
if (!_sessions.TryGetValue(token, out var entry) || entry.ExpiresAtUtc <= DateTimeOffset.UtcNow) return null;
|
||||
var current = store.GetAuthenticatedUser(entry.User.User.Id);
|
||||
if (current is null || current.User.UpdatedAtUtc != entry.User.User.UpdatedAtUtc)
|
||||
{
|
||||
_sessions.TryRemove(token, out _);
|
||||
return null;
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
public bool HasPermission(string? authorizationHeader, string permission) =>
|
||||
GetUser(authorizationHeader)?.Permissions.Contains(permission, StringComparer.Ordinal) == true;
|
||||
|
||||
public void Remove(string? authorizationHeader)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(authorizationHeader) && authorizationHeader.StartsWith("Bearer ", StringComparison.Ordinal))
|
||||
@@ -37,12 +58,14 @@ public sealed class SessionService
|
||||
private void PruneExpired()
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
foreach (var (token, expiresAt) in _sessions)
|
||||
foreach (var (token, entry) in _sessions)
|
||||
{
|
||||
if (expiresAt <= now)
|
||||
if (entry.ExpiresAtUtc <= now)
|
||||
{
|
||||
_sessions.TryRemove(token, out _);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed record SessionEntry(AuthenticatedGatewayUser User, DateTimeOffset ExpiresAtUtc);
|
||||
}
|
||||
Reference in new issue
Block a user