22using System . Collections . Generic ;
33using NSubstitute ;
44using NUnit . Framework ;
5+ using PatchKit . Api ;
56using PatchKit . Api . Models . Main ;
6- using PatchKit . Unity . Patcher . AppData . Local ;
77using PatchKit . Unity . Patcher . AppData . Remote ;
88using PatchKit . Unity . Patcher . AppUpdater . Commands ;
99using PatchKit . Unity . Patcher . Cancellation ;
1010using PatchKit . Unity . Patcher . Status ;
1111using PatchKit . Unity . Patcher . UI . Dialogs ;
12- using UnityEngine ;
1312
1413class ValidateLicenseCommandTest
1514{
15+ private PatchKit . Logging . ILogger _logger ;
16+ private IStatusMonitor _statusMonitor ;
17+ private MockCache _cache ;
18+
19+ [ SetUp ]
20+ public void SetUp ( )
21+ {
22+ _logger = Substitute . For < PatchKit . Logging . ILogger > ( ) ;
23+ _statusMonitor = Substitute . For < IStatusMonitor > ( ) ;
24+ _cache = new MockCache ( ) ;
25+ }
26+
1627 [ Test ]
17- public void Execute_UsesCachedKey ( )
28+ public void Execute_CachesKeyAndKeySecret ( )
1829 {
19- var cache = new MockCache ( ) ;
20-
2130 const string key = "this-key-should-be-cached" ;
22-
31+ const string keySecret = "this-key-secret-should-be-cached" ;
32+
2333 for ( int i = 0 ; i < 2 ; i ++ )
2434 {
2535 var licenseDialog = Substitute . For < ILicenseDialog > ( ) ;
@@ -34,23 +44,131 @@ public void Execute_UsesCachedKey()
3444 {
3545 UseKeys = true
3646 } ) ;
37-
38- var statusMonitor = Substitute . For < IStatusMonitor > ( ) ;
39-
40- var command = new ValidateLicenseCommand ( licenseDialog , remoteMetaData , cache ) ;
41- command . Prepare ( statusMonitor ) ;
47+ remoteMetaData . GetKeySecret ( key , Arg . Any < string > ( ) ) . Returns ( keySecret ) ;
48+
49+ var command = new ValidateLicenseCommand ( licenseDialog , remoteMetaData , _cache , _logger ) ;
50+ command . Prepare ( _statusMonitor ) ;
4251 command . Execute ( CancellationToken . Empty ) ;
43-
52+
4453 if ( i == 0 )
4554 {
4655 licenseDialog . Received ( 1 ) . Display ( Arg . Any < LicenseDialogMessageType > ( ) ) ;
56+ Assert . IsTrue ( _cache . Dictionary . ContainsValue ( key ) ) ;
57+ Assert . IsTrue ( _cache . Dictionary . ContainsValue ( keySecret ) ) ;
4758 }
4859 else
4960 {
61+ licenseDialog . Received ( 1 ) . SetKey ( key ) ;
5062 licenseDialog . DidNotReceive ( ) . Display ( Arg . Any < LicenseDialogMessageType > ( ) ) ;
63+ Assert . IsTrue ( _cache . Dictionary . ContainsValue ( key ) ) ;
64+ Assert . IsTrue ( _cache . Dictionary . ContainsValue ( keySecret ) ) ;
5165 }
5266 }
67+ }
68+
69+ [ Test ]
70+ public void Execute_ProperlyHandlesSitauationWhenKeysAreNotUsed ( )
71+ {
72+ var licenseDialog = Substitute . For < ILicenseDialog > ( ) ;
73+
74+ var remoteMetaData = Substitute . For < IRemoteMetaData > ( ) ;
75+ remoteMetaData . GetAppInfo ( ) . Returns ( new App ( )
76+ {
77+ UseKeys = false
78+ } ) ;
79+
80+ var command = new ValidateLicenseCommand ( licenseDialog , remoteMetaData , _cache , _logger ) ;
81+ command . Prepare ( _statusMonitor ) ;
82+ command . Execute ( CancellationToken . Empty ) ;
83+
84+ Assert . AreEqual ( command . KeySecret , null ) ;
85+ remoteMetaData . DidNotReceive ( ) . GetKeySecret ( Arg . Any < string > ( ) , Arg . Any < string > ( ) ) ;
86+ licenseDialog . DidNotReceive ( ) . Display ( Arg . Any < LicenseDialogMessageType > ( ) ) ;
87+ }
88+
89+ [ TestCase ( 404 , LicenseDialogMessageType . InvalidLicense ) ]
90+ [ TestCase ( 403 , LicenseDialogMessageType . ServiceUnavailable ) ]
91+ [ TestCase ( 410 , LicenseDialogMessageType . BlockedLicense ) ]
92+ public void Execute_DisplaysDialogMessageForApiError ( int statusCode , LicenseDialogMessageType messageType )
93+ {
94+ const string key = "key" ;
95+ const string keySecret = "key-secret" ;
96+
97+ var licenseDialog = Substitute . For < ILicenseDialog > ( ) ;
98+ licenseDialog . Display ( Arg . Any < LicenseDialogMessageType > ( ) ) . ReturnsForAnyArgs ( new LicenseDialogResult ( )
99+ {
100+ Key = key ,
101+ Type = LicenseDialogResultType . Confirmed
102+ } ) ;
103+
104+ var remoteMetaData = Substitute . For < IRemoteMetaData > ( ) ;
105+ remoteMetaData . GetAppInfo ( ) . Returns ( new App ( )
106+ {
107+ UseKeys = true
108+ } ) ;
109+
110+ bool firstAttempt = true ;
111+
112+ remoteMetaData . GetKeySecret ( key , Arg . Any < string > ( ) ) . Returns ( info =>
113+ {
114+ if ( ! firstAttempt )
115+ {
116+ return keySecret ;
117+ }
118+
119+ firstAttempt = false ;
120+ throw new ApiResponseException ( statusCode ) ;
121+ } ) ;
122+
123+ var command = new ValidateLicenseCommand ( licenseDialog , remoteMetaData , _cache , _logger ) ;
124+ command . Prepare ( _statusMonitor ) ;
125+ command . Execute ( CancellationToken . Empty ) ;
126+
127+ licenseDialog . Received ( 1 ) . Display ( LicenseDialogMessageType . None ) ;
128+ licenseDialog . Received ( 1 ) . Display ( messageType ) ;
129+ licenseDialog . DidNotReceive ( ) . Display ( Arg . Is < LicenseDialogMessageType > ( type => type != LicenseDialogMessageType . None &&
130+ type != messageType ) ) ;
131+ }
132+
133+ [ Test ]
134+ public void Execute_DisplaysProperDialogMessageForConnectionError ( )
135+ {
136+ const string key = "key" ;
137+ const string keySecret = "key-secret" ;
138+
139+ var licenseDialog = Substitute . For < ILicenseDialog > ( ) ;
140+ licenseDialog . Display ( Arg . Any < LicenseDialogMessageType > ( ) ) . ReturnsForAnyArgs ( new LicenseDialogResult ( )
141+ {
142+ Key = key ,
143+ Type = LicenseDialogResultType . Confirmed
144+ } ) ;
145+
146+ var remoteMetaData = Substitute . For < IRemoteMetaData > ( ) ;
147+ remoteMetaData . GetAppInfo ( ) . Returns ( new App ( )
148+ {
149+ UseKeys = true
150+ } ) ;
151+
152+ bool firstAttempt = true ;
153+
154+ remoteMetaData . GetKeySecret ( key , Arg . Any < string > ( ) ) . Returns ( info =>
155+ {
156+ if ( ! firstAttempt )
157+ {
158+ return keySecret ;
159+ }
160+
161+ firstAttempt = false ;
162+ throw new ApiConnectionException ( new List < Exception > ( ) , new List < Exception > ( ) ) ;
163+ } ) ;
53164
54- Assert . IsTrue ( cache . Dictionary . ContainsValue ( key ) ) ;
165+ var command = new ValidateLicenseCommand ( licenseDialog , remoteMetaData , _cache , _logger ) ;
166+ command . Prepare ( _statusMonitor ) ;
167+ command . Execute ( CancellationToken . Empty ) ;
168+
169+ licenseDialog . Received ( 1 ) . Display ( LicenseDialogMessageType . None ) ;
170+ licenseDialog . Received ( 1 ) . Display ( LicenseDialogMessageType . ServiceUnavailable ) ;
171+ licenseDialog . DidNotReceive ( ) . Display ( Arg . Is < LicenseDialogMessageType > ( type => type != LicenseDialogMessageType . None &&
172+ type != LicenseDialogMessageType . ServiceUnavailable ) ) ;
55173 }
56174}
0 commit comments