2018-09-29 22:49:12 -04:00
using IW4MAdmin.Application ;
using SharedLibraryCore ;
2018-10-02 13:39:08 -04:00
using SharedLibraryCore.Commands ;
2018-11-05 22:01:29 -05:00
using SharedLibraryCore.Database.Models ;
2018-09-29 22:49:12 -04:00
using SharedLibraryCore.Objects ;
2018-09-02 17:59:27 -04:00
using System ;
using System.Collections.Generic ;
2018-09-29 22:49:12 -04:00
using System.Linq ;
2018-09-02 17:59:27 -04:00
using System.Text ;
2018-09-29 22:49:12 -04:00
using System.Threading ;
2018-09-02 17:59:27 -04:00
using Xunit ;
namespace Tests
{
2018-09-29 22:49:12 -04:00
[Collection("ManagerCollection")]
2018-09-02 17:59:27 -04:00
public class ClientTests
{
2018-09-29 22:49:12 -04:00
readonly ApplicationManager Manager ;
2018-10-02 13:39:08 -04:00
const int TestTimeout = 10000 ;
2018-09-29 22:49:12 -04:00
public ClientTests ( ManagerFixture fixture )
{
Manager = fixture . Manager ;
}
2018-09-02 17:59:27 -04:00
[Fact]
public void SetAdditionalPropertyShouldSucceed ( )
{
2018-11-05 22:01:29 -05:00
var client = new EFClient ( ) ;
2018-09-02 17:59:27 -04:00
int newProp = 5 ;
client . SetAdditionalProperty ( "NewProp" , newProp ) ;
}
[Fact]
public void GetAdditionalPropertyShouldSucceed ( )
{
2018-11-05 22:01:29 -05:00
var client = new EFClient ( ) ;
2018-09-02 17:59:27 -04:00
int newProp = 5 ;
client . SetAdditionalProperty ( "NewProp" , newProp ) ;
Assert . True ( client . GetAdditionalProperty < int > ( "NewProp" ) = = 5 , "added property does not match retrieved property" ) ;
}
2018-09-29 22:49:12 -04:00
[Fact]
2018-10-02 13:39:08 -04:00
public void WarnClientShouldSucceed ( )
2018-09-29 22:49:12 -04:00
{
while ( ! Manager . IsInitialized )
{
Thread . Sleep ( 100 ) ;
}
2018-11-05 22:01:29 -05:00
var client = Manager . Servers . First ( ) . GetClientsAsList ( ) . FirstOrDefault ( ) ;
2018-09-29 22:49:12 -04:00
Assert . False ( client = = null , "no client found to warn" ) ;
2018-11-05 22:01:29 -05:00
var warnEvent = client . Warn ( "test warn" , new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-10-03 22:20:49 -04:00
warnEvent . OnProcessed . Wait ( ) ;
2018-09-29 22:49:12 -04:00
2018-10-03 22:20:49 -04:00
Assert . True ( ( client . Warnings = = 1 | |
warnEvent . Failed ) & &
Manager . GetPenaltyService ( ) . GetClientPenaltiesAsync ( client . ClientId ) . Result . Count ( p = > p . Type = = Penalty . PenaltyType . Warning ) = = 1 ,
"warning did not get applied" ) ;
2018-09-29 22:49:12 -04:00
2018-11-05 22:01:29 -05:00
warnEvent = client . Warn ( "test warn" , new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Banned , CurrentServer = client . CurrentServer } ) ;
2018-10-03 22:20:49 -04:00
warnEvent . OnProcessed . Wait ( ) ;
2018-09-29 22:49:12 -04:00
Assert . True ( warnEvent . FailReason = = GameEvent . EventFailReason . Permission & &
client . Warnings = = 1 , "warning was applied without proper permissions" ) ;
2018-10-02 13:39:08 -04:00
// warn clear
2018-11-05 22:01:29 -05:00
var warnClearEvent = client . WarnClear ( new EFClient { ClientId = 1 , Level = EFClient . Permission . Banned , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
Assert . True ( warnClearEvent . FailReason = = GameEvent . EventFailReason . Permission & &
client . Warnings = = 1 , "warning was removed without proper permissions" ) ;
2018-11-05 22:01:29 -05:00
warnClearEvent = client . WarnClear ( new EFClient { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
Assert . True ( ! warnClearEvent . Failed & & client . Warnings = = 0 , "warning was not cleared" ) ;
2018-09-29 22:49:12 -04:00
}
[Fact]
2018-10-02 13:39:08 -04:00
public void ReportClientShouldSucceed ( )
2018-09-29 22:49:12 -04:00
{
while ( ! Manager . IsInitialized )
{
Thread . Sleep ( 100 ) ;
}
2018-11-05 22:01:29 -05:00
var client = Manager . Servers . First ( ) . GetClientsAsList ( ) . FirstOrDefault ( ) ;
2018-09-29 22:49:12 -04:00
Assert . False ( client = = null , "no client found to report" ) ;
2018-10-03 22:20:49 -04:00
// fail
2018-11-05 22:01:29 -05:00
var player = new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ;
2018-10-03 22:20:49 -04:00
player . SetAdditionalProperty ( "_reportCount" , 3 ) ;
var reportEvent = client . Report ( "test report" , player ) ;
reportEvent . OnProcessed . Wait ( TestTimeout ) ;
Assert . True ( reportEvent . FailReason = = GameEvent . EventFailReason . Throttle &
client . CurrentServer . Reports . Count ( r = > r . Target . NetworkId = = client . NetworkId ) = = 0 , $"too many reports were applied [{reportEvent.FailReason.ToString()}]" ) ;
2018-09-29 22:49:12 -04:00
// succeed
2018-11-05 22:01:29 -05:00
reportEvent = client . Report ( "test report" , new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-09-29 22:49:12 -04:00
reportEvent . OnProcessed . Wait ( TestTimeout ) ;
Assert . True ( ! reportEvent . Failed & &
client . CurrentServer . Reports . Count ( r = > r . Target . NetworkId = = client . NetworkId ) = = 1 , $"report was not applied [{reportEvent.FailReason.ToString()}]" ) ;
// fail
2018-11-05 22:01:29 -05:00
reportEvent = client . Report ( "test report" , new EFClient ( ) { ClientId = 1 , NetworkId = 1 , Level = EFClient . Permission . Banned , CurrentServer = client . CurrentServer } ) ;
2018-09-29 22:49:12 -04:00
Assert . True ( reportEvent . FailReason = = GameEvent . EventFailReason . Permission & &
2018-10-02 13:39:08 -04:00
client . CurrentServer . Reports . Count ( r = > r . Target . NetworkId = = client . NetworkId ) = = 1 ,
$"report was applied without proper permission [{reportEvent.FailReason.ToString()},{ client.CurrentServer.Reports.Count(r => r.Target.NetworkId == client.NetworkId)}]" ) ;
2018-09-29 22:49:12 -04:00
// fail
reportEvent = client . Report ( "test report" , client ) ;
Assert . True ( reportEvent . FailReason = = GameEvent . EventFailReason . Invalid & &
client . CurrentServer . Reports . Count ( r = > r . Target . NetworkId = = client . NetworkId ) = = 1 , $"report was applied to self" ) ;
// fail
2018-11-05 22:01:29 -05:00
reportEvent = client . Report ( "test report" , new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-09-29 22:49:12 -04:00
Assert . True ( reportEvent . FailReason = = GameEvent . EventFailReason . Exception & &
client . CurrentServer . Reports . Count ( r = > r . Target . NetworkId = = client . NetworkId ) = = 1 , $"duplicate report was applied" ) ;
}
2018-10-02 13:39:08 -04:00
[Fact]
public void FlagClientShouldSucceed ( )
{
while ( ! Manager . IsInitialized )
{
Thread . Sleep ( 100 ) ;
}
2018-11-05 22:01:29 -05:00
var client = Manager . Servers . First ( ) . GetClientsAsList ( ) . FirstOrDefault ( ) ;
2018-10-02 13:39:08 -04:00
Assert . False ( client = = null , "no client found to flag" ) ;
2018-11-05 22:01:29 -05:00
var flagEvent = client . Flag ( "test flag" , new EFClient { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
flagEvent . OnProcessed . Wait ( ) ;
// succeed
Assert . True ( ! flagEvent . Failed & &
2018-11-05 22:01:29 -05:00
client . Level = = EFClient . Permission . Flagged , $"player is not flagged [{flagEvent.FailReason.ToString()}]" ) ;
2018-10-02 13:39:08 -04:00
Assert . False ( client . ReceivedPenalties . FirstOrDefault ( p = > p . Offense = = "test flag" ) = = null , "flag was not applied" ) ;
2018-11-05 22:01:29 -05:00
flagEvent = client . Flag ( "test flag" , new EFClient { ClientId = 1 , Level = EFClient . Permission . Banned , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
flagEvent . OnProcessed . Wait ( ) ;
// fail
Assert . True ( client . ReceivedPenalties . Count = = 1 , "flag was applied without permisions" ) ;
2018-11-05 22:01:29 -05:00
flagEvent = client . Flag ( "test flag" , new EFClient { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
flagEvent . OnProcessed . Wait ( ) ;
// fail
Assert . True ( client . ReceivedPenalties . Count = = 1 , "duplicate flag was applied" ) ;
2018-11-05 22:01:29 -05:00
var unflagEvent = client . Unflag ( "test unflag" , new EFClient { ClientId = 1 , Level = EFClient . Permission . Banned , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
unflagEvent . OnProcessed . Wait ( ) ;
// fail
2018-11-05 22:01:29 -05:00
Assert . False ( client . Level = = EFClient . Permission . User , "user was unflagged without permissions" ) ;
2018-10-02 13:39:08 -04:00
2018-11-05 22:01:29 -05:00
unflagEvent = client . Unflag ( "test unflag" , new EFClient { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
unflagEvent . OnProcessed . Wait ( ) ;
// succeed
2018-11-05 22:01:29 -05:00
Assert . True ( client . Level = = EFClient . Permission . User , "user was not unflagged" ) ;
2018-10-02 13:39:08 -04:00
2018-11-05 22:01:29 -05:00
unflagEvent = client . Unflag ( "test unflag" , new EFClient { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
unflagEvent . OnProcessed . Wait ( ) ;
// succeed
Assert . True ( unflagEvent . FailReason = = GameEvent . EventFailReason . Invalid , "user was not flagged" ) ;
}
[Fact]
void KickClientShouldSucceed ( )
{
while ( ! Manager . IsInitialized )
{
Thread . Sleep ( 100 ) ;
}
2018-11-05 22:01:29 -05:00
var client = Manager . Servers . First ( ) . GetClientsAsList ( ) . FirstOrDefault ( ) ;
2018-10-02 13:39:08 -04:00
Assert . False ( client = = null , "no client found to kick" ) ;
2018-11-05 22:01:29 -05:00
var kickEvent = client . Kick ( "test kick" , new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Banned , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
kickEvent . OnProcessed . Wait ( ) ;
Assert . True ( kickEvent . FailReason = = GameEvent . EventFailReason . Permission , "client was kicked without permission" ) ;
2018-11-05 22:01:29 -05:00
kickEvent = client . Kick ( "test kick" , new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ) ;
2018-10-02 13:39:08 -04:00
kickEvent . OnProcessed . Wait ( ) ;
2018-11-05 22:01:29 -05:00
Assert . True ( Manager . Servers . First ( ) . GetClientsAsList ( ) . FirstOrDefault ( c = > c . NetworkId = = client . NetworkId ) = = null , "client was not kicked" ) ;
2018-10-02 13:39:08 -04:00
}
[Fact]
void TempBanClientShouldSucceed ( )
{
while ( ! Manager . IsInitialized )
{
Thread . Sleep ( 100 ) ;
}
2018-11-05 22:01:29 -05:00
var client = Manager . Servers . First ( ) . GetClientsAsList ( ) . FirstOrDefault ( ) ;
2018-10-02 13:39:08 -04:00
Assert . False ( client = = null , "no client found to tempban" ) ;
var tbCommand = new CTempBan ( ) ;
tbCommand . ExecuteAsync ( new GameEvent ( )
{
2018-11-05 22:01:29 -05:00
Origin = new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ,
2018-10-02 13:39:08 -04:00
Target = client ,
Data = "5days test tempban" ,
Type = GameEvent . EventType . Command ,
Owner = client . CurrentServer
} ) . Wait ( ) ;
Assert . True ( Manager . GetPenaltyService ( ) . GetActivePenaltiesAsync ( client . AliasLinkId ) . Result . Count ( p = > p . Type = = Penalty . PenaltyType . TempBan ) = = 1 ,
"tempban was not added" ) ;
}
[Fact]
void BanUnbanClientShouldSucceed ( )
{
while ( ! Manager . IsInitialized )
{
Thread . Sleep ( 100 ) ;
}
2018-11-05 22:01:29 -05:00
var client = Manager . Servers . First ( ) . GetClientsAsList ( ) . FirstOrDefault ( ) ;
2018-10-02 13:39:08 -04:00
Assert . False ( client = = null , "no client found to ban" ) ;
var banCommand = new CBan ( ) ;
banCommand . ExecuteAsync ( new GameEvent ( )
{
2018-11-05 22:01:29 -05:00
Origin = new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ,
2018-10-02 13:39:08 -04:00
Target = client ,
Data = "test ban" ,
Type = GameEvent . EventType . Command ,
Owner = client . CurrentServer
} ) . Wait ( ) ;
Assert . True ( Manager . GetPenaltyService ( ) . GetActivePenaltiesAsync ( client . AliasLinkId ) . Result . Count ( p = > p . Type = = Penalty . PenaltyType . Ban ) = = 1 ,
"ban was not added" ) ;
var unbanCommand = new CUnban ( ) ;
unbanCommand . ExecuteAsync ( new GameEvent ( )
{
2018-11-05 22:01:29 -05:00
Origin = new EFClient ( ) { ClientId = 1 , Level = EFClient . Permission . Console , CurrentServer = client . CurrentServer } ,
Target = Manager . GetClientService ( ) . Find ( c = > c . NetworkId = = client . NetworkId ) . Result . First ( ) . AsEFClient ( ) ,
2018-10-02 13:39:08 -04:00
Data = "test unban" ,
Type = GameEvent . EventType . Command ,
Owner = client . CurrentServer
} ) . Wait ( ) ;
Assert . True ( Manager . GetPenaltyService ( ) . GetActivePenaltiesAsync ( client . AliasLinkId ) . Result . Count ( p = > p . Type = = Penalty . PenaltyType . Ban ) = = 0 ,
"ban was not removed" ) ;
}
2018-09-02 17:59:27 -04:00
}
}