Skip to content

unit tests: identity deserializer - #1412

Merged
AkramBitar merged 1 commit into
mainfrom
cov_identity_deserializer
Mar 12, 2026
Merged

unit tests: identity deserializer#1412
AkramBitar merged 1 commit into
mainfrom
cov_identity_deserializer

Conversation

@aaadir

@aaadir aaadir commented Mar 8, 2026

Copy link
Copy Markdown
Contributor

Improve coverage of token/services/identity/deserializer/deserializer_test.go

@aaadir aaadir self-assigned this Mar 8, 2026
@aaadir
aaadir requested review from AkramBitar and adecaro March 8, 2026 11:56
@aaadir aaadir added this to the Q1/26 milestone Mar 8, 2026
@aaadir aaadir changed the title identity deserializer unit tests unit tests: identity deserializer Mar 8, 2026
@aaadir
aaadir force-pushed the cov_identity_deserializer branch from 8aa13e5 to 67b0af4 Compare March 8, 2026 12:13
verifyFunc func(message, sigma []byte) error
}

func (m *mockVerifier) Verify(message, sigma []byte) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you check if we can use Counterfeiter to generate these mocks?

return []byte("signature"), nil
}

type mockTypedVerifierDeserializer struct {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Did you check if we can use Counterfeiter to generate these mocks?

Please check all of them

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

  • Counterfeiters for the following classes are now used instead of the previous manually defined mocks:
drivermock.Matcher
drivermock.Signer
drivermock.Verifier
drivermock.AuditInfoProvider
identitydrivermock.AuditInfo
identitydrivermock.AuditInfoDeserializer
identitydrivermock.TypedSignerDeserializer
identitydrivermock.TypedVerifierDeserializer
  • Note that for the above I had to move the definition of the interface TypedVerifierDeserializer
    to be in token/services/identity/driver/deserialization.go next to the defnition of the interface TypedSignerDeserializer.
    It used to be in the file token/services/identity/deserializer/verifier.go which includes implementations of the TypedVerifierDeserializer interface.
    This caused dependency cycles for the new token/services/identity/deserializer/deserializer_test.go
    which was resolved when I moved the interface defintion to its proper place.

@aaadir
aaadir force-pushed the cov_identity_deserializer branch from 67b0af4 to 765e0ad Compare March 11, 2026 07:42

@adecaro adecaro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please, improve the tests to check:

  • when an error is expected, to check also that the programmed errors are in there. For example, in
	t.Run("MatchError", func(t *testing.T) {
		expectedErr := errors.New("match failed")
		matcher := &drivermock.Matcher{}
		matcher.MatchReturns(expectedErr)

		typedMatcher := &TypedAuditInfoMatcher{matcher: matcher}
		typedID := createTypedIdentity(t, "test-type", []byte("raw-identity"))

		err := typedMatcher.Match(context.Background(), typedID)
		require.Error(t, err)
		assert.Contains(t, err.Error(), "failed to match identity")
	})

The test should check that expectedErr is in the error chain.

  • If no error is expected, then the expected value should be checked. For example, in
	t.Run("GetAuditInfoMatcher_Success", func(t *testing.T) {
		mockVerifierDeserializer := &drivermock.VerifierDeserializer{}

		mockMatcher := &drivermock.Matcher{}
		mockMatcherDeserializer := &drivermock.MatcherDeserializer{}
		mockMatcherDeserializer.GetAuditInfoMatcherReturns(mockMatcher, nil)

		deserializer := NewTypedIdentityVerifierDeserializer(mockVerifierDeserializer, mockMatcherDeserializer)

		typedID := createTypedIdentity(t, "test-type", []byte("raw-identity"))
		matcher, err := deserializer.GetAuditInfoMatcher(context.Background(), typedID, []byte("audit-info"))

		require.NoError(t, err)
		assert.NotNil(t, matcher)
	})

The test should check that matcher is mockMatcher.

  • counterfeiter allows the developer to check also how many times a function has been called. Please, add also the following checks.

@adecaro

adecaro commented Mar 11, 2026

Copy link
Copy Markdown
Contributor

@aaadir , please, set the status of the PR and link it to its Issue. Thanks.

@aaadir aaadir linked an issue Mar 11, 2026 that may be closed by this pull request
@aaadir
aaadir force-pushed the cov_identity_deserializer branch from 7e21588 to d6666b8 Compare March 11, 2026 11:43
@aaadir

aaadir commented Mar 11, 2026

Copy link
Copy Markdown
Contributor Author

pushed a new version addressing all the above review comments.
@adecaro - please approve (of you're ok with the requested changes).
thanks!

@aaadir
aaadir force-pushed the cov_identity_deserializer branch 3 times, most recently from 9f0f96c to c546b9c Compare March 12, 2026 05:03
@adecaro
adecaro self-requested a review March 12, 2026 15:04

@adecaro adecaro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

Signed-off-by: aaadir <adir@il.ibm.com>
@adecaro
adecaro force-pushed the cov_identity_deserializer branch from c546b9c to 3b2ae28 Compare March 12, 2026 15:04
@AkramBitar
AkramBitar merged commit 7ef8917 into main Mar 12, 2026
146 of 147 checks passed
@adecaro
adecaro deleted the cov_identity_deserializer branch April 13, 2026 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unit-test: token/services/identity/deserializer

3 participants