44
55use Illuminate \Cache \Repository ;
66use Illuminate \Foundation \Auth \User as Authenticatable ;
7+ use Illuminate \Support \Facades \Config ;
78use Illuminate \Support \Facades \Session ;
89use Illuminate \Support \Facades \Storage ;
910use Mockery \MockInterface ;
2526
2627class CredentialManagersTest extends TestCase
2728{
29+ protected function setUp (): void
30+ {
31+ parent ::setUp ();
32+
33+ Xero::$ modelStorage = null ;
34+ Xero::$ modelAttribute = 'xero_credentials ' ;
35+ Xero::$ defaultAuthGuard = 'web ' ;
36+ }
37+
2838 #[DataProvider('credentialManagers ' )]
2939 public function test_you_can_get_credential_store_without_existing_data ($ sutClass , $ setupFunction , $ createExistingData )
3040 {
@@ -195,11 +205,6 @@ public function test_that_getting_a_non_existent_tenant_will_throw_an_exception(
195205 $ this ->assertThrows (fn () => $ sut ->getTenantId (1 ), XeroTenantNotFound::class, 'No such tenant exists ' );
196206 }
197207
198- public function test_that_if_guest_it_will_throw_exception_for_authenticated_user_store ()
199- {
200- $ this ->assertThrows (fn () => new AuthenticatedUserStore (), XeroUserNotAuthenticated::class, 'User is not authenticated ' );
201- }
202-
203208 public function test_that_it_will_throw_exception_if_failed_to_write_file ()
204209 {
205210 Storage::fake ();
@@ -211,6 +216,42 @@ public function test_that_it_will_throw_exception_if_failed_to_write_file()
211216 $ this ->assertThrows (fn () => $ sut ->store (new MockAccessToken (), ['tenant ' => 'tenant_id ' , 'expires ' => 3600 ]), XeroFailedToWriteFile::class, 'Failed to write file: xero.json ' );
212217 }
213218
219+ public function test_that_it_will_throw_exception_if_not_authenticated ()
220+ {
221+ $ this ->assertGuest ();
222+
223+ $ sut = new AuthenticatedUserStore ();
224+
225+ $ this ->assertThrows (fn () => $ sut ->data (), XeroUserNotAuthenticated::class, 'User is not authenticated ' );
226+ $ this ->assertThrows (fn () => $ sut ->getAccessToken (), XeroUserNotAuthenticated::class, 'User is not authenticated ' );
227+ $ this ->assertThrows (fn () => $ sut ->getRefreshToken (), XeroUserNotAuthenticated::class, 'User is not authenticated ' );
228+ $ this ->assertThrows (fn () => $ sut ->getTenants (), XeroUserNotAuthenticated::class, 'User is not authenticated ' );
229+ $ this ->assertThrows (fn () => $ sut ->getTenantId (), XeroUserNotAuthenticated::class, 'User is not authenticated ' );
230+ $ this ->assertThrows (fn () => $ sut ->getExpires (), XeroUserNotAuthenticated::class, 'User is not authenticated ' );
231+ $ this ->assertThrows (fn () => $ sut ->getData (), XeroUserNotAuthenticated::class, 'User is not authenticated ' );
232+ $ this ->assertFalse ($ sut ->exists ());
233+ $ this ->assertThrows (fn () => $ sut ->isExpired (), XeroUserNotAuthenticated::class, 'User is not authenticated ' );
234+ $ this ->assertNull ($ sut ->getUser ());
235+ }
236+
237+ public function test_that_you_can_change_the_default_guard_for_users ()
238+ {
239+ Config::set ('auth.guards ' , array_merge (config ('auth.guards ' ), [
240+ 'admin ' => [
241+ 'driver ' => 'session ' ,
242+ 'provider ' => 'users ' ,
243+ ],
244+ ]));
245+
246+ auth ()->guard ('admin ' )->login (User::create (['xero_credentials ' => ['token ' => 'foo ' ]]));
247+
248+ $ this ->assertThrows (fn () => (new AuthenticatedUserStore ())->data (), XeroUserNotAuthenticated::class, 'User is not authenticated ' );
249+
250+ Xero::setDefaultAuthGuard ('admin ' );
251+
252+ $ this ->assertIsArray ((new AuthenticatedUserStore ())->data ());
253+ }
254+
214255 public static function credentialManagers (): array
215256 {
216257 return [
@@ -253,6 +294,10 @@ public static function credentialManagers(): array
253294
254295class User extends Authenticatable
255296{
297+ protected $ casts = [
298+ 'xero_credentials ' => 'array ' ,
299+ ];
300+
256301 protected function casts ()
257302 {
258303 return [
0 commit comments