Skip to content

Commit c69aac9

Browse files
committed
fixes based on PR comments
1 parent 33cf7be commit c69aac9

5 files changed

Lines changed: 58 additions & 55 deletions

File tree

docs/api/paths/admin/db-_user-name-_access_history-compact.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ post:
3939
compacted_channels:
4040
scope1:
4141
collection1:
42-
- scoped_channel2
42+
- scoped_channel1
4343
'400':
4444
description: Bad request. Invalid channel names or malformed request body.
4545
content:

docs/api/paths/admin/db-_user-name-_channel_history.yaml

Lines changed: 0 additions & 42 deletions
This file was deleted.

rest/admin_api.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,10 +2539,10 @@ func (h *handler) getUserChannelHistory() error {
25392539
colAccessHistoryMap[base.DefaultScope] = make(map[string][]string)
25402540
colAccessHistoryMap[base.DefaultScope][base.DefaultCollection] = slices.Collect(maps.Keys(user.ChannelHistory()))
25412541
} else {
2542-
for scope, _ := range colAccess {
2542+
for scope, cols := range colAccess {
25432543
colAccessHistoryMap[scope] = make(map[string][]string)
2544-
for col, _ := range colAccess[scope] {
2545-
colAccessHistoryMap[scope][col] = slices.Collect(maps.Keys(colAccess[scope][col].ChannelHistory_))
2544+
for col, colVal := range cols {
2545+
colAccessHistoryMap[scope][col] = slices.Collect(maps.Keys(colVal.ChannelHistory_))
25462546
}
25472547
}
25482548
}
@@ -2567,17 +2567,17 @@ func (h *handler) compactUserChannelHistory() error {
25672567
username := internalUserName(mux.Vars(h.rq)["name"])
25682568
authenticator := h.db.Authenticator(h.ctx())
25692569
user, err := authenticator.GetUser(username)
2570-
if user == nil {
2571-
if err == nil {
2572-
err = kNotFoundError
2573-
}
2570+
if err != nil {
25742571
return err
25752572
}
2573+
if user == nil {
2574+
return kNotFoundError
2575+
}
25762576

25772577
colAccessHistoryMap := make(map[string]map[string][]string)
2578-
for scope, _ := range reqUserChannelHistory.Channels {
2578+
for scope, cols := range reqUserChannelHistory.Channels {
25792579
colAccessHistoryMap[scope] = make(map[string][]string)
2580-
for col, _ := range reqUserChannelHistory.Channels[scope] {
2580+
for col, _ := range cols {
25812581
colAccessHistoryMap[scope][col] = user.CompactChannelHistory(scope, col, reqUserChannelHistory.Channels[scope][col])
25822582
}
25832583
}

rest/revocation_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,8 @@ func TestRevocationWithAdminChannels(t *testing.T) {
903903
assert.Equal(t, "doc", changes.Results[0].ID)
904904
assert.True(t, changes.Results[0].Revoked)
905905

906-
body := fmt.Sprintf(`{"channels": {%q:{%q:["A"]}}}'`, dataStore.ScopeName(), dataStore.CollectionName())
907-
resp = rt.SendAdminRequest("POST", "/db/_user/user/_channel_history", body)
906+
body := fmt.Sprintf(`{"channels": {%q:{%q:["A"]}}}`, dataStore.ScopeName(), dataStore.CollectionName())
907+
resp = rt.SendAdminRequest("POST", "/db/_user/user/_access_history/compact", body)
908908
RequireStatus(t, resp, http.StatusOK)
909909

910910
changes = rt.WaitForChanges(1, fmt.Sprintf("/{{.keyspace}}/_changes?since=%d&revocations=true", 2), "user", false)

rest/user_api_test.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,51 @@ func TestGetUserChannelHistory(t *testing.T) {
17011701
assert.ElementsMatch(t, []string{"chan1", "chan2"}, channelHistory[scope][collection])
17021702
})
17031703

1704+
// User has channels revoked in two named collections (same scope). Both collections and their respective revoked channels should appear in _channel_history.
1705+
t.Run("MultipleNamedCollections", func(t *testing.T) {
1706+
base.RequireNumTestDataStores(t, 2)
1707+
rtMulti := NewRestTesterMultipleCollections(t, nil, 2)
1708+
defer rtMulti.Close()
1709+
1710+
scope := rtMulti.GetDbCollections()[0].ScopeName
1711+
col1 := rtMulti.GetDbCollections()[0].Name
1712+
col2 := rtMulti.GetDbCollections()[1].Name
1713+
1714+
userPayload := fmt.Sprintf(`{
1715+
"password": "letmein",
1716+
"collection_access": {
1717+
%q: {
1718+
%q: {"admin_channels": ["chan1", "chan2"]},
1719+
%q: {"admin_channels": ["chan3", "chan4"]}
1720+
}
1721+
}
1722+
}`, scope, col1, col2)
1723+
response := rtMulti.SendAdminRequest(http.MethodPut, "/db/_user/user1", userPayload)
1724+
RequireStatus(t, response, http.StatusCreated)
1725+
1726+
// Revoke all channels in both collections
1727+
revokePayload := fmt.Sprintf(`{
1728+
"collection_access": {
1729+
%q: {
1730+
%q: {"admin_channels": []},
1731+
%q: {"admin_channels": []}
1732+
}
1733+
}
1734+
}`, scope, col1, col2)
1735+
response = rtMulti.SendAdminRequest(http.MethodPut, "/db/_user/user1", revokePayload)
1736+
RequireStatus(t, response, http.StatusOK)
1737+
1738+
response = rtMulti.SendAdminRequest(http.MethodGet, "/db/_user/user1/_access_history", "")
1739+
RequireStatus(t, response, http.StatusOK)
1740+
1741+
var result map[string]ColAccessHistoryMap
1742+
require.NoError(t, base.JSONUnmarshal(response.Body.Bytes(), &result))
1743+
1744+
channelHistory := result["channels"]
1745+
assert.ElementsMatch(t, []string{"chan1", "chan2"}, channelHistory[scope][col1])
1746+
assert.ElementsMatch(t, []string{"chan3", "chan4"}, channelHistory[scope][col2])
1747+
})
1748+
17041749
// Revocation history is preserved even after a previously revoked channel is re-granted.
17051750
t.Run("ChannelHistoryAfterReGrant", func(t *testing.T) {
17061751
ds := rt.GetSingleDataStore()
@@ -1731,7 +1776,7 @@ func TestGetUserChannelHistory(t *testing.T) {
17311776
})
17321777
}
17331778

1734-
// TestCompactUserChannelHistory tests the POST /_user/{name}/_channel_history admin endpoint,
1779+
// TestCompactUserChannelHistory tests the POST /_user/{name}/_access_history/compact admin endpoint,
17351780
// which removes specified channels from a user's revocation history and returns those that were found and removed.
17361781
func TestCompactUserChannelHistory(t *testing.T) {
17371782
rt := NewRestTester(t, nil)

0 commit comments

Comments
 (0)