passwordless-server icon indicating copy to clipboard operation
passwordless-server copied to clipboard

Add some tests

Open pooya1380m opened this issue 1 year ago • 7 comments

Added Tests to SharedManagementServiceTests for CreateApiKeyAsync method

and I like to know what should I use instead of

https://github.com/bitwarden/passwordless-server/blob/1f47bf111e4f4e6bb2aaee632006219e8cd135b8/src/Service/Storage/Ef/DbTenantFactory.cs#L14

pooya1380m avatar Feb 28 '24 10:02 pooya1380m

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.

jonashendrickx avatar Feb 28 '24 12:02 jonashendrickx

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

pooya1380m avatar Feb 28 '24 12:02 pooya1380m

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 :-)

jonashendrickx avatar Feb 28 '24 12:02 jonashendrickx

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 avatar Feb 28 '24 12:02 pooya1380m

@jonashendrickx can you review this?

pooya1380m avatar Feb 29 '24 05:02 pooya1380m

@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 avatar Feb 29 '24 07:02 jonashendrickx

@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

pooya1380m avatar Mar 01 '24 12:03 pooya1380m