test: CGS commit and detach negative tests#38173
Conversation
WalkthroughThis pull request introduces two new test classes in the Appsmith server's Git operations testing suite: Changes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
📜 Recent review detailsConfiguration used: .coderabbit.yaml 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (6)
app/server/appsmith-server/src/test/java/com/appsmith/server/git/ops/GitDetachTests.java (3)
61-86: Consider enhancing test robustness.While the test logic is sound, consider these improvements:
- Add a more descriptive error message in the StepVerifier
- Add test cleanup to remove the created workspace and application
StepVerifier.create(applicationMono) .expectErrorMatches(throwable -> throwable instanceof AppsmithException - & throwable.getMessage().contains("Git configuration is invalid")) + & throwable.getMessage().contains("Git configuration is invalid"), + "Should fail when Git data is empty") .verify(); + + // Clean up + workspaceService.archiveById(workspace.getId()).block();
88-101: Add explicit exception type verification.The test verifies the error message but should also explicitly verify the exception type for better test coverage.
StepVerifier.create(applicationMono) + .expectError(AppsmithException.class) .expectErrorMessage( AppsmithError.ACL_NO_RESOURCE_FOUND.getMessage(FieldName.APPLICATION, application.getId())) .verify();
109-130: Consider using reactive chains instead of blocking operations.While blocking operations are acceptable in tests, consider using reactive chains for consistency with the production code style.
-User apiUser = userService.findByEmail("api_user").block(); +return userService.findByEmail("api_user") + .flatMap(apiUser -> { + Workspace toCreate = new Workspace(); + toCreate.setName("Workspace_" + UUID.randomUUID()); + return workspaceService.create(toCreate, apiUser, Boolean.FALSE); + }) + .flatMap(workspace -> { + Application testApplication = new Application(); + testApplication.setWorkspaceId(workspace.getId()); + testApplication.setName("Test App"); + return applicationPageService.createApplication(testApplication); + }) + .flatMap(application -> { + application.getPolicyMap().remove(permission.getValue()); + return applicationRepository.save(application); + });app/server/appsmith-server/src/test/java/com/appsmith/server/git/ops/GitCommitTests.java (3)
46-48: Add class-level documentationAdd Javadoc to describe the test class's purpose and the scenarios it covers.
@SpringBootTest @TestInstance(TestInstance.Lifecycle.PER_CLASS) +/** + * Tests for Git commit operations in CentralGitService, covering: + * - Empty commit scenarios + * - Invalid Git configuration + * - Repository not found cases + */ public class GitCommitTests {
71-109: Extract test setup to improve readabilityConsider extracting the workspace and application setup into a separate helper method since it's likely to be reused in other tests.
+ private Workspace createTestWorkspace(String prefix) { + User apiUser = userService.findByEmail("api_user").block(); + Workspace toCreate = new Workspace(); + toCreate.setName(prefix + "_" + UUID.randomUUID()); + return workspaceService.create(toCreate, apiUser, Boolean.FALSE).block(); + } @Test @WithUserDetails(value = "api_user") public void commitArtifact_whenNoChangesInLocal_returnsWithEmptyCommitMessage() { CommitDTO commitDTO = new CommitDTO(); commitDTO.setMessage("empty commit"); - User apiUser = userService.findByEmail("api_user").block(); - Workspace toCreate = new Workspace(); - toCreate.setName("Workspace_" + UUID.randomUUID()); - Workspace workspace = workspaceService.create(toCreate, apiUser, Boolean.FALSE).block(); + Workspace workspace = createTestWorkspace("Workspace");
198-203: Use more precise error validationInstead of using
expectErrorMatches, consider usingexpectErrorSatisfiesfor more precise error validation.- StepVerifier.create(commitMono) - .expectErrorMatches(throwable -> throwable instanceof AppsmithException - && throwable - .getMessage() - .equals(AppsmithError.INVALID_GIT_CONFIGURATION.getMessage(GIT_CONFIG_ERROR))) - .verify(); + StepVerifier.create(commitMono) + .expectErrorSatisfies(throwable -> { + assertThat(throwable).isInstanceOf(AppsmithException.class); + assertThat(throwable.getMessage()) + .isEqualTo(AppsmithError.INVALID_GIT_CONFIGURATION.getMessage(GIT_CONFIG_ERROR)); + }) + .verify();
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
app/server/appsmith-server/src/test/java/com/appsmith/server/git/ops/GitCommitTests.java(1 hunks)app/server/appsmith-server/src/test/java/com/appsmith/server/git/ops/GitDetachTests.java(1 hunks)
🔇 Additional comments (1)
app/server/appsmith-server/src/test/java/com/appsmith/server/git/ops/GitDetachTests.java (1)
1-60: Well-structured test class with proper dependency injection.
The class follows Spring Boot testing best practices with appropriate annotations and dependency injection setup.
| private Application createApplicationConnectedToGit(String name, String branchName, String workspaceId) | ||
| throws IOException { | ||
|
|
||
| if (StringUtils.isEmpty(branchName)) { | ||
| branchName = "foo"; | ||
| } | ||
| Mockito.doReturn(Mono.just(branchName)) | ||
| .when(fsGitHandler) | ||
| .cloneRemoteIntoArtifactRepo(any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString()); | ||
| Mockito.doReturn(Mono.just("commit")) | ||
| .when(fsGitHandler) | ||
| .commitArtifact( | ||
| any(Path.class), | ||
| Mockito.anyString(), | ||
| Mockito.anyString(), | ||
| Mockito.anyString(), | ||
| Mockito.anyBoolean(), | ||
| Mockito.anyBoolean()); | ||
| Mockito.doReturn(Mono.just(true)).when(fsGitHandler).checkoutToBranch(any(Path.class), Mockito.anyString()); | ||
| Mockito.doReturn(Mono.just("success")) | ||
| .when(fsGitHandler) | ||
| .pushApplication(any(Path.class), any(), any(), any(), any()); | ||
| Mockito.doReturn(Mono.just(true)).when(commonGitFileUtils).checkIfDirectoryIsEmpty(any(Path.class)); | ||
| Mockito.doReturn(Mono.just(Paths.get("textPath"))) | ||
| .when(commonGitFileUtils) | ||
| .initializeReadme(any(Path.class), Mockito.anyString(), Mockito.anyString()); | ||
| Mockito.doReturn(Mono.just(Paths.get("path"))) | ||
| .when(commonGitFileUtils) | ||
| .saveArtifactToLocalRepoWithAnalytics(any(Path.class), any(), Mockito.anyString()); | ||
|
|
||
| Application testApplication = new Application(); | ||
| testApplication.setName(name); | ||
| testApplication.setWorkspaceId(workspaceId); | ||
| Application application1 = | ||
| applicationPageService.createApplication(testApplication).block(); | ||
|
|
||
| GitArtifactMetadata gitArtifactMetadata = new GitArtifactMetadata(); | ||
| GitAuth gitAuth = new GitAuth(); | ||
| gitAuth.setPublicKey("testkey"); | ||
| gitAuth.setPrivateKey("privatekey"); | ||
| gitArtifactMetadata.setGitAuth(gitAuth); | ||
| gitArtifactMetadata.setDefaultApplicationId(application1.getId()); | ||
| gitArtifactMetadata.setRepoName("testRepo"); | ||
| application1.setGitApplicationMetadata(gitArtifactMetadata); | ||
| application1 = applicationService.save(application1).block(); | ||
|
|
||
| PageDTO page = new PageDTO(); | ||
| page.setName("New Page"); | ||
| page.setApplicationId(application1.getId()); | ||
| applicationPageService.createPage(page).block(); | ||
|
|
||
| GitProfile gitProfile = new GitProfile(); | ||
| gitProfile.setAuthorEmail("test@email.com"); | ||
| gitProfile.setAuthorName("testUser"); | ||
| GitConnectDTO gitConnectDTO = new GitConnectDTO(); | ||
| gitConnectDTO.setRemoteUrl("git@github.com:test/testRepo.git"); | ||
| gitConnectDTO.setGitProfile(gitProfile); | ||
| return centralGitService | ||
| .connectArtifactToGit( | ||
| application1.getId(), gitConnectDTO, "baseUrl", ArtifactType.APPLICATION, GitType.FILE_SYSTEM) | ||
| .map(artifact -> (Application) artifact) | ||
| .block(); | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Split helper method into smaller focused methods
The helper method is doing too much. Consider splitting it into:
- Application creation
- Git metadata setup
- Mock configuration
+ private void configureMocks(String branchName) {
+ Mockito.doReturn(Mono.just(branchName))
+ .when(fsGitHandler)
+ .cloneRemoteIntoArtifactRepo(any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
+ // ... other mock configurations
+ }
+
+ private Application setupGitMetadata(Application application) {
+ GitArtifactMetadata gitArtifactMetadata = new GitArtifactMetadata();
+ GitAuth gitAuth = new GitAuth();
+ gitAuth.setPublicKey("testkey");
+ gitAuth.setPrivateKey("privatekey");
+ // ... rest of the metadata setup
+ return applicationService.save(application).block();
+ }Committable suggestion skipped: line range outside the PR's diff.
Failed server tests
|
## Description > [!TIP] > _Add a TL;DR when the description is longer than 500 words or extremely technical (helps the content, marketing, and DevRel team)._ > > _Please also include relevant motivation and context. List any dependencies that are required for this change. Add links to Notion, Figma or any other documents that might be relevant to the PR._ Fixes #`Issue Number` _or_ Fixes `Issue URL` > [!WARNING] > _If no issue exists, please create an issue first, and check with the maintainers if the issue is valid._ ## Automation /ok-to-test tags="" ### 🔍 Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!WARNING] > Tests have not run on the HEAD c7743a7 yet > <hr>Mon, 16 Dec 2024 07:21:41 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [ ] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced unit tests for Git commit functionality, covering scenarios such as empty commit messages and invalid configurations. - Added unit tests for Git detach functionality, validating error handling and permission checks. - **Tests** - New test classes created to enhance testing coverage for Git operations. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Description
Tip
Add a TL;DR when the description is longer than 500 words or extremely technical (helps the content, marketing, and DevRel team).
Please also include relevant motivation and context. List any dependencies that are required for this change. Add links to Notion, Figma or any other documents that might be relevant to the PR.
Fixes #
Issue Numberor
Fixes
Issue URLWarning
If no issue exists, please create an issue first, and check with the maintainers if the issue is valid.
Automation
/ok-to-test tags=""
🔍 Cypress test results
Warning
Tests have not run on the HEAD c7743a7 yet
Mon, 16 Dec 2024 07:21:41 UTC
Communication
Should the DevRel and Marketing teams inform users about this change?
Summary by CodeRabbit
New Features
Tests