Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F120822355
CompanionAppsTest.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
10 KB
Referenced Files
None
Subscribers
None
CompanionAppsTest.php
View Options
<?php
namespace
Tests\Feature\Controller
;
use
App\CompanionApp
;
use
App\Utils
;
use
Laravel\Passport\Passport
;
use
Laravel\Passport\Token
;
use
Laravel\Passport\TokenRepository
;
use
Tests\TestCase
;
class
CompanionAppsTest
extends
TestCase
{
protected
function
setUp
():
void
{
parent
::
setUp
();
$this
->
deleteTestUser
(
'CompanionAppsTest1@userscontroller.com'
);
$this
->
deleteTestUser
(
'CompanionAppsTest2@userscontroller.com'
);
$this
->
deleteTestCompanionApp
(
'testdevice'
);
}
protected
function
tearDown
():
void
{
$this
->
deleteTestUser
(
'CompanionAppsTest1@userscontroller.com'
);
$this
->
deleteTestUser
(
'CompanionAppsTest2@userscontroller.com'
);
$this
->
deleteTestCompanionApp
(
'testdevice'
);
parent
::
tearDown
();
}
/**
* Test creating the app
*/
public
function
testStore
():
void
{
$user
=
$this
->
getTestUser
(
'CompanionAppsTest1@userscontroller.com'
);
$name
=
"testname"
;
$post
=
[
'name'
=>
$name
];
$response
=
$this
->
actingAs
(
$user
)->
post
(
"api/v4/companions"
,
$post
);
$response
->
assertStatus
(
200
);
$json
=
$response
->
json
();
$this
->
assertCount
(
3
,
$json
);
$this
->
assertSame
(
'success'
,
$json
[
'status'
]);
$this
->
assertSame
(
"Companion app has been created."
,
$json
[
'message'
]);
$companionApp
=
CompanionApp
::
where
(
'name'
,
$name
)->
first
();
$this
->
assertTrue
(
$companionApp
!=
null
);
$this
->
assertSame
(
$name
,
$companionApp
->
name
);
$this
->
assertFalse
((
bool
)
$companionApp
->
mfa_enabled
);
}
/**
* Test destroying the app
*/
public
function
testDestroy
():
void
{
$user
=
$this
->
getTestUser
(
'CompanionAppsTest1@userscontroller.com'
);
$user2
=
$this
->
getTestUser
(
'CompanionAppsTest2@userscontroller.com'
);
$response
=
$this
->
actingAs
(
$user
)->
delete
(
"api/v4/companions/foobar"
);
$response
->
assertStatus
(
404
);
$companionApp
=
$this
->
getTestCompanionApp
(
'testdevice'
,
$user
,
[
'notification_token'
=>
'notificationtoken'
,
'mfa_enabled'
=>
1
,
'name'
=>
'testname'
,
]
);
$client
=
Passport
::
client
()->
forceFill
([
'user_id'
=>
$user
->
id
,
'name'
=>
"CompanionApp Password Grant Client"
,
'secret'
=>
"VerySecret"
,
'provider'
=>
'users'
,
'redirect'
=>
'https://'
.
\config
(
'app.website_domain'
),
'personal_access_client'
=>
0
,
'password_client'
=>
1
,
'revoked'
=>
false
,
'allowed_scopes'
=>
[
"mfa"
],
]);
$client
->
save
();
$companionApp
->
oauth_client_id
=
$client
->
id
;
$companionApp
->
save
();
$tokenRepository
=
app
(
TokenRepository
::
class
);
$tokenRepository
->
create
([
'id'
=>
'testtoken'
,
'revoked'
=>
false
,
'user_id'
=>
$user
->
id
,
'client_id'
=>
$client
->
id
,
]);
// Make sure we have a token to revoke
$tokenCount
=
Token
::
where
(
'user_id'
,
$user
->
id
)->
where
(
'client_id'
,
$client
->
id
)->
count
();
$this
->
assertTrue
(
$tokenCount
>
0
);
$response
=
$this
->
actingAs
(
$user2
)->
delete
(
"api/v4/companions/{$companionApp->id}"
);
$response
->
assertStatus
(
403
);
$response
=
$this
->
actingAs
(
$user
)->
delete
(
"api/v4/companions/{$companionApp->id}"
);
$response
->
assertStatus
(
200
);
$json
=
$response
->
json
();
$this
->
assertCount
(
2
,
$json
);
$this
->
assertSame
(
'success'
,
$json
[
'status'
]);
$this
->
assertSame
(
"Companion app has been removed."
,
$json
[
'message'
]);
$client
->
refresh
();
$this
->
assertSame
((
bool
)
$client
->
revoked
,
true
);
$companionApp
=
CompanionApp
::
where
(
'device_id'
,
'testdevice'
)->
first
();
$this
->
assertTrue
(
$companionApp
==
null
);
$tokenCount
=
Token
::
where
(
'user_id'
,
$user
->
id
)
->
where
(
'client_id'
,
$client
->
id
)
->
where
(
'revoked'
,
false
)->
count
();
$this
->
assertSame
(
0
,
$tokenCount
);
}
/**
* Test listing apps
*/
public
function
testIndex
():
void
{
$response
=
$this
->
get
(
"api/v4/companions"
);
$response
->
assertStatus
(
401
);
$user
=
$this
->
getTestUser
(
'CompanionAppsTest1@userscontroller.com'
);
$companionApp
=
$this
->
getTestCompanionApp
(
'testdevice'
,
$user
,
[
'notification_token'
=>
'notificationtoken'
,
'mfa_enabled'
=>
1
,
'name'
=>
'testname'
,
]
);
$response
=
$this
->
actingAs
(
$user
)->
get
(
"api/v4/companions"
);
$response
->
assertStatus
(
200
);
$json
=
$response
->
json
();
$this
->
assertSame
(
1
,
$json
[
'count'
]);
$this
->
assertCount
(
1
,
$json
[
'list'
]);
$this
->
assertSame
(
$user
->
id
,
$json
[
'list'
][
0
][
'user_id'
]);
$this
->
assertSame
(
$companionApp
[
'device_id'
],
$json
[
'list'
][
0
][
'device_id'
]);
$this
->
assertSame
(
$companionApp
[
'name'
],
$json
[
'list'
][
0
][
'name'
]);
$this
->
assertSame
(
$companionApp
[
'notification_token'
],
$json
[
'list'
][
0
][
'notification_token'
]);
$this
->
assertSame
(
$companionApp
[
'mfa_enabled'
],
$json
[
'list'
][
0
][
'mfa_enabled'
]);
$user2
=
$this
->
getTestUser
(
'CompanionAppsTest2@userscontroller.com'
);
$response
=
$this
->
actingAs
(
$user2
)->
get
(
"api/v4/companions"
);
$response
->
assertStatus
(
200
);
$json
=
$response
->
json
();
$this
->
assertSame
(
0
,
$json
[
'count'
]);
$this
->
assertCount
(
0
,
$json
[
'list'
]);
}
/**
* Test showing the app
*/
public
function
testShow
():
void
{
$user
=
$this
->
getTestUser
(
'CompanionAppsTest1@userscontroller.com'
);
$companionApp
=
$this
->
getTestCompanionApp
(
'testdevice'
,
$user
);
$response
=
$this
->
get
(
"api/v4/companions/{$companionApp->id}"
);
$response
->
assertStatus
(
401
);
$response
=
$this
->
actingAs
(
$user
)->
get
(
"api/v4/companions/aaa"
);
$response
->
assertStatus
(
404
);
$response
=
$this
->
actingAs
(
$user
)->
get
(
"api/v4/companions/{$companionApp->id}"
);
$response
->
assertStatus
(
200
);
$json
=
$response
->
json
();
$this
->
assertSame
(
$companionApp
->
id
,
$json
[
'id'
]);
$user2
=
$this
->
getTestUser
(
'CompanionAppsTest2@userscontroller.com'
);
$response
=
$this
->
actingAs
(
$user2
)->
get
(
"api/v4/companions/{$companionApp->id}"
);
$response
->
assertStatus
(
403
);
}
/**
* Test registering the app
*/
public
function
testRegister
():
void
{
$user
=
$this
->
getTestUser
(
'CompanionAppsTest1@userscontroller.com'
);
$companionApp
=
$this
->
getTestCompanionApp
(
'testdevice'
,
$user
,
[
'notification_token'
=>
'notificationtoken'
,
'mfa_enabled'
=>
0
,
'name'
=>
'testname'
,
]
);
$notificationToken
=
"notificationToken"
;
$deviceId
=
"deviceId"
;
$name
=
"testname"
;
$response
=
$this
->
actingAs
(
$user
)->
post
(
"api/v4/companion/register"
,
[
'notificationToken'
=>
$notificationToken
,
'deviceId'
=>
$deviceId
,
'name'
=>
$name
,
'companionId'
=>
$companionApp
->
id
,
]
);
$response
->
assertStatus
(
200
);
$companionApp
->
refresh
();
$this
->
assertTrue
(
$companionApp
!=
null
);
$this
->
assertSame
(
$deviceId
,
$companionApp
->
device_id
);
$this
->
assertSame
(
$name
,
$companionApp
->
name
);
$this
->
assertSame
(
$notificationToken
,
$companionApp
->
notification_token
);
$this
->
assertTrue
((
bool
)
$companionApp
->
mfa_enabled
);
// Companion id required
$response
=
$this
->
actingAs
(
$user
)->
post
(
"api/v4/companion/register"
,
[
'notificationToken'
=>
$notificationToken
,
'deviceId'
=>
$deviceId
,
'name'
=>
$name
]
);
$response
->
assertStatus
(
422
);
// Test a token update
$notificationToken
=
"notificationToken2"
;
$response
=
$this
->
actingAs
(
$user
)->
post
(
"api/v4/companion/register"
,
[
'notificationToken'
=>
$notificationToken
,
'deviceId'
=>
$deviceId
,
'name'
=>
$name
,
'companionId'
=>
$companionApp
->
id
,
]
);
$response
->
assertStatus
(
200
);
$companionApp
->
refresh
();
$this
->
assertSame
(
$notificationToken
,
$companionApp
->
notification_token
);
// Failing input valdiation
$response
=
$this
->
actingAs
(
$user
)->
post
(
"api/v4/companion/register"
,
[]
);
$response
->
assertStatus
(
422
);
// Other users device
$user2
=
$this
->
getTestUser
(
'CompanionAppsTest2@userscontroller.com'
);
$response
=
$this
->
actingAs
(
$user2
)->
post
(
"api/v4/companion/register"
,
[
'notificationToken'
=>
$notificationToken
,
'deviceId'
=>
$deviceId
,
'name'
=>
$name
,
'companionId'
=>
$companionApp
->
id
,
]
);
$response
->
assertStatus
(
403
);
}
/**
* Test getting the pairing info
*/
public
function
testPairing
():
void
{
$user
=
$this
->
getTestUser
(
'CompanionAppsTest1@userscontroller.com'
);
$companionApp
=
$this
->
getTestCompanionApp
(
'testdevice'
,
$user
,
[
'notification_token'
=>
'notificationtoken'
,
'mfa_enabled'
=>
0
,
'name'
=>
'testname'
,
]
);
$response
=
$this
->
get
(
"api/v4/companions/{$companionApp->id}/pairing"
);
$response
->
assertStatus
(
401
);
$response
=
$this
->
actingAs
(
$user
)->
get
(
"api/v4/companions/{$companionApp->id}/pairing"
);
$response
->
assertStatus
(
200
);
$json
=
$response
->
json
();
$client
=
$companionApp
->
fresh
()->
passportClient
();
$this
->
assertTrue
(
$client
!==
null
);
$this
->
assertSame
(
$user
->
email
,
$json
[
'username'
]);
$this
->
assertSame
(
$companionApp
->
id
,
$json
[
'companionId'
]);
$this
->
assertSame
(
$client
->
id
,
$json
[
'clientIdentifier'
]);
// TODO: This should be clientId
$this
->
assertSame
(
$client
->
secret
,
$json
[
'clientSecret'
]);
$this
->
assertSame
(
Utils
::
serviceUrl
(
''
,
$user
->
tenant_id
),
$json
[
'serverUrl'
]);
$this
->
assertArrayHasKey
(
'qrcode'
,
$json
);
$this
->
assertSame
(
'data:image/svg+xml;base64,'
,
substr
(
$json
[
'qrcode'
],
0
,
26
));
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Fri, Apr 24, 9:48 AM (1 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18856449
Default Alt Text
CompanionAppsTest.php (10 KB)
Attached To
Mode
rK kolab
Attached
Detach File
Event Timeline