11using System . Net ;
22using System . Net . Http . Json ;
3+ using System . Net . Mime ;
34using Microsoft . AspNetCore . Hosting ;
45using Microsoft . EntityFrameworkCore ;
56using Microsoft . Extensions . DependencyInjection ;
6- using Newtonsoft . Json ;
77using OpenShock . API . Models . Response ;
8- using OpenShock . Common . Models ;
98using OpenShock . Common . OpenShockDb ;
109using OpenShock . Common . Redis ;
1110using OpenShock . Common . Utils ;
1211using Redis . OM . Contracts ;
13- using JsonSerializer = System . Text . Json . JsonSerializer ;
1412
1513namespace OpenShock . API . IntegrationTests . Tests ;
1614
@@ -19,106 +17,100 @@ public sealed class LcgAssignmentTests
1917 [ ClassDataSource < WebApplicationFactory > ( Shared = SharedType . PerTestSession ) ]
2018 public required WebApplicationFactory WebApplicationFactory { get ; init ; }
2119
22- private static readonly Guid UserId = Guid . Parse ( "11111111-1111-1111-1111-111111111111" ) ;
23- private static readonly Guid HubId = Guid . Parse ( "11111111-1111-1111-1111-111111111111" ) ;
24- private const string HubToken = "test" ;
20+ private Guid _userId ;
21+ private Guid _hubId ;
22+ private string _hubToken = string . Empty ;
2523
2624 [ Before ( Test ) ]
2725 public async Task Setup ( )
2826 {
27+ // Dependency Resolution
2928 await using var context = WebApplicationFactory . Services . CreateAsyncScope ( ) ;
3029 var db = context . ServiceProvider . GetRequiredService < OpenShockContext > ( ) ;
30+ var redisConnectionProvider = context . ServiceProvider . GetRequiredService < IRedisConnectionProvider > ( ) ;
31+ var webHostEnvironment = context . ServiceProvider . GetRequiredService < IWebHostEnvironment > ( ) ;
32+ var lcgNodesCollection = redisConnectionProvider . RedisCollection < LcgNode > ( saveState : true ) ;
3133
32- var user = new User
33- {
34- Id = UserId ,
35- Name = "TestUser" ,
36- Email = "test@test.org" ,
37- PasswordHash = HashingUtils . HashPassword ( "password" ) ,
38- CreatedAt = DateTime . UtcNow ,
39- ActivatedAt = DateTime . UtcNow
40- } ;
34+ // Set up variables
35+ _userId = Guid . CreateVersion7 ( ) ;
36+ _hubId = Guid . CreateVersion7 ( ) ;
37+ _hubToken = CryptoUtils . RandomAlphaNumericString ( 256 ) ;
4138
42- db . Users . Add ( user ) ;
43-
44- var hub = new Device
39+ // Create mock data
40+ db . Users . Add ( new User
4541 {
46- Id = HubId ,
42+ Id = _userId ,
43+ Name = _userId . ToString ( "N" ) ,
44+ Email = $ "{ _userId } @test.org",
45+ PasswordHash = HashingUtils . HashPassword ( "password" )
46+ } ) ;
47+ db . Devices . Add ( new Device
48+ {
49+ Id = _hubId ,
4750 Name = "TestHub" ,
48- OwnerId = UserId ,
49- Token = HubToken ,
51+ OwnerId = _userId ,
52+ Token = _hubToken ,
5053 CreatedAt = DateTime . UtcNow
51- } ;
54+ } ) ;
55+ await db . SaveChangesAsync ( ) ;
5256
53- db . Devices . Add ( hub ) ;
57+ ( string country , string fqdn ) [ ] availableGateways = [
58+ ( "US" , "us1.example.com" ) ,
59+ ( "DE" , "de1.example.com" ) ,
60+ ( "AS" , "as1.example.com" )
61+ ] ;
5462
55- await db . SaveChangesAsync ( ) ;
63+ await lcgNodesCollection . InsertAsync ( availableGateways . Select ( x => new LcgNode
64+ {
65+ Country = x . country ,
66+ Fqdn = x . fqdn ,
67+ Load = 0 ,
68+ Environment = webHostEnvironment . EnvironmentName
69+ } ) ) ;
5670 }
5771
5872 [ After ( Test ) ]
5973 public async Task Teardown ( )
6074 {
75+ // Dependency Resolution
6176 await using var context = WebApplicationFactory . Services . CreateAsyncScope ( ) ;
6277 var db = context . ServiceProvider . GetRequiredService < OpenShockContext > ( ) ;
63- await db . Devices . Where ( x => x . Id == HubId ) . ExecuteDeleteAsync ( ) ;
64- await db . Users . Where ( x => x . Id == UserId ) . ExecuteDeleteAsync ( ) ;
65-
6678 var redisConnectionProvider = context . ServiceProvider . GetRequiredService < IRedisConnectionProvider > ( ) ;
67- var webHostEnvironment = context . ServiceProvider . GetRequiredService < IWebHostEnvironment > ( ) ;
6879 var lcgNodesCollection = redisConnectionProvider . RedisCollection < LcgNode > ( false ) ;
69-
80+
81+ // Data cleanup
82+ await db . Devices . Where ( x => x . Id == _hubId ) . ExecuteDeleteAsync ( ) ;
83+ await db . Users . Where ( x => x . Id == _userId ) . ExecuteDeleteAsync ( ) ;
84+
7085 var allLcg = await lcgNodesCollection . ToArrayAsync ( ) ;
7186 await lcgNodesCollection . DeleteAsync ( allLcg ) ;
7287 }
7388
7489 [ Test ]
75- [ NotInParallel ]
76- [ Arguments ( "US" , "us1.example.com" , new [ ] { "US|us1.example.com" , "DE|de1.example.com" , "AS|as1.example.com" } ) ]
77- [ Arguments ( "DE" , "de1.example.com" , new [ ] { "US|us1.example.com" , "DE|de1.example.com" , "AS|as1.example.com" } ) ]
78- [ Arguments ( "CA" , "us1.example.com" , new [ ] { "US|us1.example.com" , "DE|de1.example.com" , "AS|as1.example.com" } ) ]
79- [ Arguments ( "CA" , "us1.example.com" , new [ ] { "US|us1.example.com" , "DE|de1.example.com" , "AS|as1.example.com" } ) ]
80- [ Arguments ( "AT" , "de1.example.com" , new [ ] { "US|us1.example.com" , "DE|de1.example.com" , "AS|as1.example.com" } ) ]
81- [ Arguments ( "FR" , "de1.example.com" , new [ ] { "US|us1.example.com" , "DE|de1.example.com" , "AS|as1.example.com" } ) ]
82- public async Task GetLcgAssignment ( string requesterCountry , string expectedHost , string [ ] availableGateways )
90+ [ Arguments ( "US" , "us1.example.com" ) ]
91+ [ Arguments ( "DE" , "de1.example.com" ) ]
92+ [ Arguments ( "CA" , "us1.example.com" ) ]
93+ [ Arguments ( "CA" , "us1.example.com" ) ]
94+ [ Arguments ( "AT" , "de1.example.com" ) ]
95+ [ Arguments ( "FR" , "de1.example.com" ) ]
96+ public async Task GetLcgAssignment ( string requesterCountry , string expectedHost )
8397 {
8498 using var client = WebApplicationFactory . CreateClient ( ) ;
8599
86100 await using var context = WebApplicationFactory . Services . CreateAsyncScope ( ) ;
87- var redisConnectionProvider = context . ServiceProvider . GetRequiredService < IRedisConnectionProvider > ( ) ;
88- var webHostEnvironment = context . ServiceProvider . GetRequiredService < IWebHostEnvironment > ( ) ;
89- var lcgNodesCollection = redisConnectionProvider . RedisCollection < LcgNode > ( false ) ;
90-
91- var testGateways = availableGateways . Select ( x =>
92- {
93- var split = x . Split ( '|' ) ;
94- if ( split . Length != 2 )
95- throw new ArgumentException ( "Invalid gateway format" ) ;
96-
97- return new LcgNode
98- {
99- Country = split [ 0 ] ,
100- Fqdn = split [ 1 ] ,
101- Load = 0 ,
102- Environment = webHostEnvironment . EnvironmentName
103- } ;
104- } ) ;
105-
106- await lcgNodesCollection . InsertAsync ( testGateways ) ;
107101
108102 var httpRequest = new HttpRequestMessage ( HttpMethod . Get , "/2/device/assignLCG?version=1" ) ;
109- httpRequest . Headers . Add ( "Device-Token" , HubToken ) ;
103+ httpRequest . Headers . Add ( "Device-Token" , _hubToken ) ;
110104 httpRequest . Headers . Add ( "CF-IPCountry" , requesterCountry ) ;
111105 var response = await client . SendAsync ( httpRequest ) ;
112106
113107 await Assert . That ( response . StatusCode ) . IsEqualTo ( HttpStatusCode . OK ) ;
114108
115109 var mediaType = response . Content . Headers . ContentType ? . MediaType ;
116- await Assert . That ( mediaType ) . IsEqualTo ( "application/json" ) ;
110+ await Assert . That ( mediaType ) . IsEqualTo ( MediaTypeNames . Application . Json ) ;
117111
118112 var data = await response . Content . ReadFromJsonAsync < LcgNodeResponseV2 > ( ) ;
119113 await Assert . That ( data ) . IsNotNull ( ) ;
120114 await Assert . That ( data . Host ) . IsEqualTo ( expectedHost ) ;
121115 }
122-
123-
124116}
0 commit comments