firestore: fix incorrect validation on CollectionGroup queries

CollectionGroup queries should be able to use snapshot cursors from
other collections. This is allowed by the API but is prevented by
the client library currently. See similar fix in the python client
here: https://github.com/googleapis/google-cloud-python/pull/8882

Fixes #1659

Change-Id: I71882e5981b97484a8222b47225a75d6da72fc6d
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/49150
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: BenWhitehead <benwhitehead@google.com>
Reviewed-by: Jean de Klerk <deklerk@google.com>
diff --git a/firestore/query.go b/firestore/query.go
index ac98060..9df9ecc 100644
--- a/firestore/query.go
+++ b/firestore/query.go
@@ -397,12 +397,11 @@
 }
 
 func (q *Query) docSnapshotToCursorValues(ds *DocumentSnapshot, orders []order) ([]*pb.Value, error) {
-	// TODO(jba): error if doc snap does not belong to the right collection.
 	vals := make([]*pb.Value, len(orders))
 	for i, ord := range orders {
 		if ord.isDocumentID() {
 			dp, qp := ds.Ref.Parent.Path, q.path
-			if dp != qp {
+			if !q.allDescendants && dp != qp {
 				return nil, fmt.Errorf("firestore: document snapshot for %s passed to query on %s", dp, qp)
 			}
 			vals[i] = &pb.Value{ValueType: &pb.Value_ReferenceValue{ds.Ref.Path}}
diff --git a/firestore/query_test.go b/firestore/query_test.go
index e31c05b..2ab23ea 100644
--- a/firestore/query_test.go
+++ b/firestore/query_test.go
@@ -743,22 +743,25 @@
 	subDoc := subColl.Doc("sub-doc")
 	subSubColl := subDoc.Collection("sub-sub-collection")
 	subSubDoc := subSubColl.Doc("sub-sub-doc")
+	collGroup := c.CollectionGroup("collection-group")
 
 	testCases := []struct {
-		queryColl      *CollectionRef
+		queryColl      *Query
 		queryFilterDoc *DocumentRef // startAt or endBefore
 		wantColl       string
 		wantRef        string
 		wantErr        bool
 	}{
 		// Queries are allowed at depth 0.
-		{parentColl, parentDoc, "parent-collection", "projects/P/databases/DB/documents/parent-collection/parent-doc", false},
+		{parentColl.query(), parentDoc, "parent-collection", "projects/P/databases/DB/documents/parent-collection/parent-doc", false},
 		// Queries are allowed at any depth.
-		{subColl, subDoc, "sub-collection", "projects/P/databases/DB/documents/parent-collection/parent-doc/sub-collection/sub-doc", false},
-		// Queries must be on immediate children (not allowed on grandchildren).
-		{subColl, someOtherParentDoc, "", "", true},
+		{subColl.query(), subDoc, "sub-collection", "projects/P/databases/DB/documents/parent-collection/parent-doc/sub-collection/sub-doc", false},
+		// Queries must be on immediate children (not allowed on grandchildren),
+		// except for CollectionGroup queries.
+		{subColl.query(), someOtherParentDoc, "", "", true},
+		{collGroup.query(), someOtherParentDoc, "collection-group", "projects/P/databases/DB/documents/parent-collection/some-other-parent-doc", false},
 		// Queries must be on immediate children (not allowed on siblings).
-		{subColl, subSubDoc, "", "", true},
+		{subColl.query(), subSubDoc, "", "", true},
 	}
 
 	// startAt
@@ -783,7 +786,7 @@
 		}
 		want := &pb.StructuredQuery{
 			From: []*pb.StructuredQuery_CollectionSelector{
-				{CollectionId: testCase.wantColl},
+				{CollectionId: testCase.wantColl, AllDescendants: testCase.queryColl.allDescendants},
 			},
 			OrderBy: []*pb.StructuredQuery_Order{
 				{Field: fref1("a"), Direction: pb.StructuredQuery_ASCENDING},
@@ -825,7 +828,7 @@
 		}
 		want := &pb.StructuredQuery{
 			From: []*pb.StructuredQuery_CollectionSelector{
-				{CollectionId: testCase.wantColl},
+				{CollectionId: testCase.wantColl, AllDescendants: testCase.queryColl.allDescendants},
 			},
 			OrderBy: []*pb.StructuredQuery_Order{
 				{Field: fref1("a"), Direction: pb.StructuredQuery_ASCENDING},