Skip to content

Add comprehensive Crypt feature tests (contract-aligned success + failure coverage)#108

Merged
hwbrzzl merged 7 commits intomasterfrom
copilot/add-full-tests-for-crypt-module
Mar 14, 2026
Merged

Add comprehensive Crypt feature tests (contract-aligned success + failure coverage)#108
hwbrzzl merged 7 commits intomasterfrom
copilot/add-full-tests-for-crypt-module

Conversation

Copy link

Copilot AI commented Mar 14, 2026

  • Review new PR comments and determine actionable items
  • Check recent GitHub Actions failures on master and current branch
  • Apply requested test suite embedding change in crypt tests
  • Run targeted crypt feature tests
  • Reply to actionable comment with commit hash
Original prompt

This section details on the original issue you should resolve

<issue_title>Add full tests for the Crypt module</issue_title>
<issue_description>### Before feedback (在反馈之前)

  • There are no features that I want to submit in Issues (当前 Issues 中没有我将要提交的新功能)

Describe Feature (描述功能)

Before feedback (在反馈之前)

  • There are no features that I want to submit in Issues (当前 Issues 中没有我将要提交的新功能)

Describe Feature (描述功能)

Before feedback (在反馈之前)

  • There are no features that I want to submit in Issues (当前 Issues 中没有我将要提交的新功能)

Describe Feature (描述功能)

We want to add integration tests for goravel/framework's features to ensure they work as expected.

  1. Add full tests for the Cache module in goravel/example based on the interface: https://github.com/goravel/framework/tree/master/contracts/crypt.
  2. The tests can be added in https://github.com/goravel/example/tree/master/tests/feature. Feel free to add a controller in https://github.com/goravel/example/tree/master/app/http/controllers if a request is required.
  3. Let tests fail if there are bugs in the module.
  4. Create a branch for the PR based on the master branch, and merge the PR to the master branch.</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Main Features

Comprehensive Crypt Module Test Suite

Introduces a feature test suite for the Crypt module (CryptTestSuite) with complete contract-aligned coverage for encryption and decryption workflows, including:

  • Round-trip encryption/decryption validation: Verifies that plaintext encrypted and then decrypted returns the original value
  • Input diversity: Tests with ASCII strings, empty strings, and Unicode characters
  • Non-deterministic encryption: Ensures the same plaintext encrypted multiple times produces different ciphertexts
  • Error handling and edge cases: Validates decryption failures for invalid/tampered payloads and structural violations

Examples

Round-trip validation with diverse inputs:

func (s *CryptTestSuite) TestEncryptAndDecryptString() {
	values := []string{
		"goravel",
		"",
		"你好,Goravel 🚀",
	}

	for _, value := range values {
		s.Run(value, func() {
			encrypted, err := facades.Crypt().EncryptString(value)
			s.NoError(err)
			s.NotEmpty(encrypted)
			s.NotEqual(value, encrypted)

			decrypted, err := facades.Crypt().DecryptString(encrypted)
			s.NoError(err)
			s.Equal(value, decrypted)
		})
	}
}

Non-deterministic encryption verification:

func (s *CryptTestSuite) TestEncryptStringIsRandomized() {
	first, err := facades.Crypt().EncryptString("goravel")
	s.NoError(err)

	second, err := facades.Crypt().EncryptString("goravel")
	s.NoError(err)

	s.NotEqual(first, second)
}

Error path coverage for malformed and tampered payloads:

func (s *CryptTestSuite) TestDecryptStringErrors() {
	// Invalid base64/non-JSON payloads
	_, err := facades.Crypt().DecryptString("invalid-payload")
	s.Error(err)

	// Missing required fields (iv, value)
	invalidWithoutIV, _ := json.Marshal(map[string][]byte{"value": []byte("ciphertext")})
	_, err = facades.Crypt().DecryptString(base64.StdEncoding.EncodeToString(invalidWithoutIV))
	s.Error(err)
	s.ErrorContains(err, "iv")

	// Tampered ciphertext (bitwise modification)
	encrypted, _ := facades.Crypt().EncryptString("goravel")
	decoded, _ := base64.StdEncoding.DecodeString(encrypted)
	var payload map[string][]byte
	json.Unmarshal(decoded, &payload)
	payload["value"][0] ^= 0xFF
	tamperedPayload, _ := json.Marshal(payload)
	_, err = facades.Crypt().DecryptString(base64.StdEncoding.EncodeToString(tamperedPayload))
	s.Error(err)
}

The implementation uses testify/suite pattern, allows subtest identification by plaintext value, and exercises the facades.Crypt() interface methods for detecting regressions in the underlying Crypt module.

Copilot AI and others added 5 commits March 14, 2026 08:23
Co-authored-by: hwbrzzl <24771476+hwbrzzl@users.noreply.github.com>
Co-authored-by: hwbrzzl <24771476+hwbrzzl@users.noreply.github.com>
Co-authored-by: hwbrzzl <24771476+hwbrzzl@users.noreply.github.com>
Co-authored-by: hwbrzzl <24771476+hwbrzzl@users.noreply.github.com>
Co-authored-by: hwbrzzl <24771476+hwbrzzl@users.noreply.github.com>
Copilot AI changed the title [WIP] Add full tests for the Crypt module Add comprehensive Crypt feature tests (contract-aligned success + failure coverage) Mar 14, 2026
Copilot AI requested a review from hwbrzzl March 14, 2026 08:32
@hwbrzzl hwbrzzl marked this pull request as ready for review March 14, 2026 08:34
@hwbrzzl hwbrzzl requested a review from a team as a code owner March 14, 2026 08:34
@hwbrzzl
Copy link
Contributor

hwbrzzl commented Mar 14, 2026

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Mar 14, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai
Copy link

coderabbitai bot commented Mar 14, 2026

📝 Walkthrough

Walkthrough

A new test suite for the Crypt module is added, covering encryption/decryption cycles with various inputs, verifying encryption randomization, and validating error handling for malformed payloads and tampering.

Changes

Cohort / File(s) Summary
Crypt Module Tests
tests/feature/crypt_test.go
New test suite with three test cases: encrypts/decrypts various values including empty and Unicode strings, ensures encryption produces unique ciphertexts for the same plaintext, and validates error handling for invalid or tampered encrypted payloads.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding comprehensive feature tests for the Crypt module with both success and failure coverage, which matches the file addition of tests/feature/crypt_test.go.
Linked Issues check ✅ Passed The PR fulfills all coding requirements from issue #929: comprehensive tests for EncryptString and DecryptString in tests/feature/crypt_test.go covering success cases (round-trip correctness, input diversity, randomization) and failure cases (invalid/malformed payloads, tampering).
Out of Scope Changes check ✅ Passed All changes are scoped to adding the crypt_test.go feature test file; no unrelated modifications or out-of-scope changes are present in the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch copilot/add-full-tests-for-crypt-module
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
tests/feature/crypt_test.go (2)

28-39: Consider using a descriptive name for the empty string subtest.

When value is an empty string (line 24), s.Run(value, ...) creates a subtest with an empty name, which produces unclear test output like TestEncryptAndDecryptString/. A descriptive name would improve test readability.

✨ Proposed improvement for subtest naming
+func subtestName(value string) string {
+	if value == "" {
+		return "empty_string"
+	}
+	return value
+}
+
 func (s *CryptTestSuite) TestEncryptAndDecryptString() {
 	values := []string{
 		"goravel",
 		"",
 		"你好,Goravel 🚀",
 	}

 	for _, value := range values {
-		s.Run(value, func() {
+		s.Run(subtestName(value), func() {
 			encrypted, err := facades.Crypt().EncryptString(value)

Alternatively, use a struct-based approach with explicit names:

testCases := []struct {
	name  string
	value string
}{
	{"ascii_string", "goravel"},
	{"empty_string", ""},
	{"unicode_string", "你好,Goravel 🚀"},
}
for _, tc := range testCases {
	s.Run(tc.name, func() {
		// use tc.value
	})
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/feature/crypt_test.go` around lines 28 - 39, The subtest names passed
to s.Run are the raw value strings, causing an empty name when value == "" and
producing unclear test output; update the loop that calls s.Run to provide
descriptive names (e.g., map empty string to "empty_string" or switch to a slice
of structs like testCases with explicit name and value) and keep using
facades.Crypt().EncryptString and facades.Crypt().DecryptString inside the
subtest body so behavior is unchanged while s.Run receives a non-empty,
descriptive name for each case.

13-15: Consider embedding tests.TestCase for consistency with other test suites.

Most test suites in tests/feature/ (including AuthTestSuite, DBTestSuite, OrmTestSuite, etc.) embed both suite.Suite and tests.TestCase. Only CryptTestSuite and CacheTestSuite deviate from this pattern. While the tests function correctly as-is (bootstrap is handled by TestMain in main_test.go), conforming to the established pattern would improve consistency.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/feature/crypt_test.go` around lines 13 - 15, Embed the shared test
helper into the CryptTestSuite so it matches other suites: update the struct
definition of CryptTestSuite to include tests.TestCase alongside suite.Suite
(the same pattern used by AuthTestSuite, DBTestSuite, OrmTestSuite, etc.),
ensuring any setup/teardown provided by tests.TestCase is available to
CryptTestSuite (and mirror the change if CacheTestSuite should also conform).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@tests/feature/crypt_test.go`:
- Around line 28-39: The subtest names passed to s.Run are the raw value
strings, causing an empty name when value == "" and producing unclear test
output; update the loop that calls s.Run to provide descriptive names (e.g., map
empty string to "empty_string" or switch to a slice of structs like testCases
with explicit name and value) and keep using facades.Crypt().EncryptString and
facades.Crypt().DecryptString inside the subtest body so behavior is unchanged
while s.Run receives a non-empty, descriptive name for each case.
- Around line 13-15: Embed the shared test helper into the CryptTestSuite so it
matches other suites: update the struct definition of CryptTestSuite to include
tests.TestCase alongside suite.Suite (the same pattern used by AuthTestSuite,
DBTestSuite, OrmTestSuite, etc.), ensuring any setup/teardown provided by
tests.TestCase is available to CryptTestSuite (and mirror the change if
CacheTestSuite should also conform).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 58d9e217-3ae1-43a4-a468-9560220baf68

📥 Commits

Reviewing files that changed from the base of the PR and between b89d64e and 76bbf1e.

📒 Files selected for processing (1)
  • tests/feature/crypt_test.go

Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

Co-authored-by: hwbrzzl <24771476+hwbrzzl@users.noreply.github.com>
Copilot AI requested a review from hwbrzzl March 14, 2026 09:32
@hwbrzzl hwbrzzl merged commit 1cce166 into master Mar 14, 2026
3 of 4 checks passed
@hwbrzzl hwbrzzl deleted the copilot/add-full-tests-for-crypt-module branch March 14, 2026 13:24
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.

2 participants