1

I have following service method

public async Task<IResultList<IEnrichedContentEntity>> QueryAsync(Context context, string schemaIdOrName, Q q)
        {
            Guard.NotNull(context, nameof(context));

            if (q == null)
            {
                return EmptyContents;
            }

            var schema = await GetSchemaOrThrowAsync(context, schemaIdOrName);
            var permissionReadOwn = Permissions.ForApp(Permissions.AppContentReadOwn, context.App.Name, schemaIdOrName);

            if (context.Permissions.Allows(permissionReadOwn))
            {
                q.CreatedBy = context.User.Token();
            }

            using (Profiler.TraceMethod<ContentQueryService>())
            {
                q = await queryParser.ParseAsync(context, q, schema);

                var contents = await contentRepository.QueryAsync(context.App, schema, q, context.Scope());

                if (q.Ids != null && q.Ids.Count > 0)
                {
                    contents = contents.SortSet(x => x.Id, q.Ids);
                }

                return await TransformAsync(context, contents);
            }
        }

q.CreatedBy must set value if permission is correct. How can I test if q.CreatedBy is not empty and has correct value.

I implemented following test, but no idea how check this params?

public async Task QueryAll_should_return_own_user_contents(int isFrontend, int unpublished, SearchScope scope)
        {
            var ctx = CreateContextWithOwnReadPermission(isFrontend: isFrontend == 1, allowSchema: true)
                    .WithUnpublished(unpublished == 1);

            var content = CreateContent(contentId);

            var q = Q.Empty.WithReference(DomainId.NewGuid());

            A.CallTo(() => contentRepository.QueryAsync(ctx.App, schema, q, scope))
              .Returns(ResultList.CreateFrom(5, content));

            //A.CallTo(() => contentRepository.QueryAsync(ctx.App, schema, A<Q>.That.Matches(x => x.CreatedBy == null), scope))
              //  .MustHaveHappened();

            //A.CallTo(() => contentRepository.QueryAsync(ctx.App, schema, A<Q>.That.Matches(x => x.CreatedBy == ctx.User.Token()), scope))
              //  .MustHaveHappened();

            //q.CreatedBy = ctx.User.Token();

            var result = await sut.QueryAsync(ctx, schemaId.Name, q);

            Assert.Equal(contentData, result[0].Data);
            Assert.Equal(contentId, result[0].Id);

            Assert.Equal(5, result.Total);
        }
Stefan Hansch
  • 1,550
  • 4
  • 22
  • 55
  • 1
    `Assert.Equal( q.CreatedBy, expectedCreatedBy );` ?? – Fildor Jan 08 '21 at 12:27
  • ^^ not sure if expected is 1. param? Anyway, passed in `q`'s property should have been modified by the call, so you should be able to check its state after the call. – Fildor Jan 08 '21 at 12:34
  • Heads up: `q = await queryParser.ParseAsync(context, q, schema);` if this function does not return the initial instance, you could encounter unexpected values. – Fildor Jan 08 '21 at 12:40
  • I can't use just `Assert.Equal( q.CreatedBy, expectedCreatedBy );` because q is just param of method. Method QueryAsync no have ref or out param. – Stefan Hansch Jan 08 '21 at 12:53
  • 2
    Like Obama said: "Yes you can!". Since `q` seems to be a reference type, you _will_ see mutations in the caller after the function has returned. Yes, it's "by copy" but the copy is the copy of the reference, not the object itself. – Fildor Jan 08 '21 at 12:59
  • Easy proof: https://dotnetfiddle.net/7Dq3il – Fildor Jan 08 '21 at 13:08
  • thanks for you suport – Stefan Hansch Jan 08 '21 at 18:18

0 Answers0