(t *testing.T)
| 18 | ) |
| 19 | |
| 20 | func TestDynamicChannelGrant(t *testing.T) { |
| 21 | |
| 22 | base.SetUpTestLogging(t, base.LevelInfo, base.KeyAccess) |
| 23 | |
| 24 | db, ctx := setupTestDB(t) |
| 25 | defer db.Close(ctx) |
| 26 | dbCollection, ctx := GetSingleDatabaseCollectionWithUser(ctx, t, db) |
| 27 | syncFn := ` |
| 28 | function(doc) { |
| 29 | if(doc.type == "setaccess") { |
| 30 | channel(doc.channel); |
| 31 | access(doc.owner, doc.channel); |
| 32 | } else { |
| 33 | channel(doc.channel) |
| 34 | } |
| 35 | }` |
| 36 | dbCollection.ChannelMapper = channels.NewChannelMapper(ctx, syncFn, db.Options.JavascriptTimeout) |
| 37 | |
| 38 | a := dbCollection.Authenticator(ctx) |
| 39 | user, err := a.NewUser("user1", "letmein", nil) |
| 40 | assert.NoError(t, err) |
| 41 | assert.NoError(t, a.Save(user)) |
| 42 | dbCollection.user = user |
| 43 | |
| 44 | // Create a document in channel chan1 |
| 45 | doc1Body := Body{"channel": "chan1", "greeting": "hello"} |
| 46 | _, _, err = dbCollection.PutExistingRevWithBody(ctx, "doc1", doc1Body, []string{"1-a"}, false, ExistingVersionWithUpdateToHLV) |
| 47 | require.NoError(t, err) |
| 48 | |
| 49 | // Verify user cannot access document |
| 50 | existingBody, err := dbCollection.Get1xBody(ctx, "doc1") |
| 51 | require.Error(t, err) |
| 52 | require.Nil(t, existingBody) |
| 53 | |
| 54 | // Write access granting document |
| 55 | grantingBody := Body{"type": "setaccess", "owner": "user1", "channel": "chan1"} |
| 56 | _, _, err = dbCollection.PutExistingRevWithBody(ctx, "grant1", grantingBody, []string{"1-a"}, false, ExistingVersionWithUpdateToHLV) |
| 57 | require.NoError(t, err) |
| 58 | |
| 59 | // Verify reloaded user can access document |
| 60 | require.NoError(t, dbCollection.ReloadUser(ctx)) |
| 61 | existingBody, err = dbCollection.Get1xBody(ctx, "doc1") |
| 62 | require.NoError(t, err) |
| 63 | require.NotNil(t, existingBody) |
| 64 | assert.Equal(t, "hello", existingBody["greeting"]) |
| 65 | |
| 66 | // Create a document in channel chan2 |
| 67 | doc2Body := Body{"channel": "chan2", "greeting": "hello"} |
| 68 | _, _, err = dbCollection.PutExistingRevWithBody(ctx, "doc2", doc2Body, []string{"1-a"}, false, ExistingVersionWithUpdateToHLV) |
| 69 | require.NoError(t, err) |
| 70 | |
| 71 | // Write access granting document for chan2 (tests invalidation when channels/inval_seq exists) |
| 72 | grantingBody = Body{"type": "setaccess", "owner": "user1", "channel": "chan2"} |
| 73 | _, _, err = dbCollection.PutExistingRevWithBody(ctx, "grant2", grantingBody, []string{"1-a"}, false, ExistingVersionWithUpdateToHLV) |
| 74 | require.NoError(t, err) |
| 75 | |
| 76 | // Verify user can now access both documents |
| 77 | require.NoError(t, dbCollection.ReloadUser(ctx)) |
nothing calls this directly
no test coverage detected