Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict who can use the lastSeenAt user sort #2634

Merged
merged 9 commits into from
Mar 2, 2021
Prev Previous commit
Next Next commit
Merge branch 'master' into cw/user-last-seen-sort-fix
  • Loading branch information
askvortsov1 authored Mar 2, 2021
commit 4118013f8ed239c97f36352fcc37ca626a4a40c7
2 changes: 2 additions & 0 deletions src/Api/Controller/ListUsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ protected function data(ServerRequestInterface $request, Document $document)
}

$query = Arr::get($this->extractFilter($request), 'q');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might have messed up the merge. I think this line has disappeared from master


$filters = $this->extractFilter($request);
$sort = $this->extractSort($request);

$limit = $this->extractLimit($request);
Expand Down
196 changes: 196 additions & 0 deletions tests/integration/api/users/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ public function shows_index_for_admin()

$this->assertEquals(200, $response->getStatusCode());
}

/**
* @test
*/
public function shows_full_results_without_search_or_filter()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])
);

$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['1', '2'], Arr::pluck($data, 'id'));
}

/**
* @test
Expand All @@ -96,6 +112,24 @@ public function disallows_last_seen_sorting_without_permission()
$this->assertEquals(400, $response->getStatusCode());
}

/**
* @test
*/
public function group_filter_works()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['group' => '1'],
])
);

$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['1'], Arr::pluck($data, 'id'));
}

/**
* @test
*/
Expand All @@ -116,5 +150,167 @@ public function allows_last_seen_sorting_with_permission()
);

$this->assertEquals(200, $response->getStatusCode());
}

/**
* @test
*/
public function group_filter_works_negated()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['-group' => '1'],
])
);

$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['2'], Arr::pluck($data, 'id'));
}

/**
* @test
*/
public function email_filter_works()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['email' => 'admin@machine.local'],
])
);

$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['1'], Arr::pluck($data, 'id'));
}

/**
* @test
*/
public function email_filter_works_negated()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['-email' => 'admin@machine.local'],
])
);

$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['2'], Arr::pluck($data, 'id'));
}

/**
* @test
*/
public function email_filter_only_works_for_admin()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 2,
])->withQueryParams([
'filter' => ['email' => 'admin@machine.local'],
])
);

$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['1', '2'], Arr::pluck($data, 'id'));
}

/**
* @test
*/
public function group_gambit_works()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['q' => 'group:1'],
])
);

$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['1'], Arr::pluck($data, 'id'));
}

/**
* @test
*/
public function group_gambit_works_negated()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['q' => '-group:1'],
])
);

$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['2'], Arr::pluck($data, 'id'));
}

/**
* @test
*/
public function email_gambit_works()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['q' => 'email:admin@machine.local'],
])
);

$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['1'], Arr::pluck($data, 'id'));
}

/**
* @test
*/
public function email_gambit_works_negated()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 1,
])->withQueryParams([
'filter' => ['q' => '-email:admin@machine.local'],
])
);

$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals(['2'], Arr::pluck($data, 'id'));
}

/**
* @test
*/
public function email_gambit_only_works_for_admin()
{
$response = $this->send(
$this->request('GET', '/api/users', [
'authenticatedAs' => 2,
])->withQueryParams([
'filter' => ['q' => 'email:admin@machine.local'],
])
);

$this->assertEquals(200, $response->getStatusCode());
$data = json_decode($response->getBody()->getContents(), true)['data'];
$this->assertEquals([], Arr::pluck($data, 'id'));
}
}
You are viewing a condensed version of this merge commit. You can view the full changes here.