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

Add some tests #450

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open

Add some tests #450

wants to merge 17 commits into from

Conversation

pooya1380m
Copy link
Contributor

Added Tests to SharedManagementServiceTests for CreateApiKeyAsync method

and I like to know what should I use instead of

ITenantStorage Create(string appId);

@pooya1380m pooya1380m requested a review from a team as a code owner February 28, 2024 10:51
@jonashendrickx
Copy link
Member

jonashendrickx commented Feb 28, 2024

In your tests, you need to use the ITenantStorageFactory, and mock it to return an ITenantStorage mock.

Then for

var tenantStorageMock = new Mock<ITenantStorage>(); you can mock any method to return something you choose.

Directly using the ITenantStorage anywhere in your application assumes the public key or private key are valid and have populated the relevant claim to instantiate ITenantStorage with TenantProvider. But you don't have to worry about TenantProvider as it is being populated automatically from the claims once authenticated.

When API keys are being validated, you will always have to use the factory for this reason.

@pooya1380m
Copy link
Contributor Author

What do you mean you want to know what you'd like instead of the factory method?

[Obsolete("Use injected ITenantStorage instead when not calling from ManagementKey scope.")]
its obsolete

@jonashendrickx
Copy link
Member

What do you mean you want to know what you'd like instead of the factory method?

[Obsolete("Use injected ITenantStorage instead when not calling from ManagementKey scope.")] its obsolete

See my comment above :-)

@pooya1380m
Copy link
Contributor Author

What do you mean you want to know what you'd like instead of the factory method?

[Obsolete("Use injected ITenantStorage instead when not calling from ManagementKey scope.")] its obsolete

See my comment above :-)

thanks

@pooya1380m
Copy link
Contributor Author

@jonashendrickx
can you review this?

@jonashendrickx
Copy link
Member

jonashendrickx commented Feb 29, 2024

@jonashendrickx can you review this?

Can you run dotnet format in your working directory and push again?

Looks good overall, you could theoretically also verify you're calling StoreApiKey with the parameters you expect to be passed. I've gotten bitten often enough by mappings that were wrong.

It could look like:

storageMock.Verify(x => x.StoreApiKeyAsync(It.Is<string>(p => p == pk), It.Is<string>(p => p == publicKey), It.Is<PublicKeyScopes[]>(p => p == scopes)), Times.Once);

.Be("appId:public:");

storageMock.Verify(x => x.StoreApiKeyAsync(It.IsAny<string>(), It.IsAny<string>(),
It.Is<string[]>(p => p == scopes.Select(s => s.GetValue()).ToArray())), Times.Once);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jonashendrickx it don't work I don't know why

@pooya1380m
Copy link
Contributor Author

@jonashendrickx can you review this?

Can you run dotnet format in your working directory and push again?

Looks good overall, you could theoretically also verify you're calling StoreApiKey with the parameters you expect to be passed. I've gotten bitten often enough by mappings that were wrong.

It could look like:

storageMock.Verify(x => x.StoreApiKeyAsync(It.Is<string>(p => p == pk), It.Is<string>(p => p == publicKey), It.Is<PublicKeyScopes[]>(p => p == scopes)), Times.Once);

@jonashendrickx

I don't have the pk and publicKey in arrange level of test, the guid is generated inside of SetupApiKeyAsync method
how can I have them as expected ?

I wrote it like this

 storageMock.Verify(x => x.StoreApiKeyAsync(It.IsAny<string>(), It.IsAny<string>(),
            It.Is<string[]>(p => p == scopes.Select(s => s.GetValue()).ToArray())), Times.Once);

but it give this error " Expected invocation on the mock once, but was 0 times" I don't know why
can you check this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants