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 assertions tests #1411

Merged
merged 10 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void testModuleScreen() throws Exception {
);
CommandListResponseBean commandResponse = getNextScreenForEofNavigation(submitResponse,
CommandListResponseBean.class);
assert commandResponse.getCommands().length == 18;
assert commandResponse.getCommands().length == 19;
assert commandResponse.getTitle().equals("Basic Tests");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.commcare.formplayer.tests;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.commcare.formplayer.application.MenuController;
import org.commcare.formplayer.beans.menus.BaseResponseBean;
import org.commcare.formplayer.beans.menus.CommandListResponseBean;
import org.commcare.formplayer.configuration.CacheConfiguration;
import org.commcare.formplayer.junit.InitializeStaticsExtension;
import org.commcare.formplayer.junit.Installer;
import org.commcare.formplayer.junit.RestoreFactoryExtension;
import org.commcare.formplayer.junit.StorageFactoryExtension;
import org.commcare.formplayer.junit.request.Response;
import org.commcare.formplayer.junit.request.SessionNavigationRequest;
import org.commcare.formplayer.services.RestoreFactory;
import org.commcare.formplayer.utils.MockRequestUtils;
import org.commcare.formplayer.utils.TestContext;
import org.commcare.formplayer.web.client.WebClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;

@WebMvcTest
@Import({MenuController.class})
@ContextConfiguration(classes = {TestContext.class, CacheConfiguration.class})
@ExtendWith(InitializeStaticsExtension.class)
public class MenuAssertionsAlternateRestoreTest {

private static final String ASSERTIONS_APP = "basic";
@Autowired
protected WebClient webClientMock;

@Autowired
RestoreFactory restoreFactoryMock;

@Autowired
private MockMvc mockMvc;

private MockRequestUtils mockRequest;

@RegisterExtension
static RestoreFactoryExtension restoreFactoryExt = new RestoreFactoryExtension.builder()
.withUser("user").withDomain("domain")
.withRestorePath("restores/project_role_admin.xml")
.build();

@RegisterExtension
static StorageFactoryExtension storageExt = new StorageFactoryExtension.builder()
.withUser("user").withDomain("domain").build();

@BeforeEach
public void setUp() throws Exception {
mockRequest = new MockRequestUtils(webClientMock, restoreFactoryMock);
}


@Test
public void testMenuAssertions() {
// Check passing assertion for project_role user property set
Response<CommandListResponseBean> response1 = navigate(new String[]{"0"},
CommandListResponseBean.class);
assertNotNull(response1);

// Check failing assertion for menu exclusively for admins, project_role=admin
try {
navigate(new String[]{"18"},
CommandListResponseBean.class);
} catch (Exception e) {
System.out.println("error message: " + e.getMessage());
assertTrue(e.getMessage().contains("This menu is for admins only."));
}
}


private <T extends BaseResponseBean> Response<T> navigate(
String[] selections, Class<T> responseClass) {
String installReference = Installer.getInstallReference(ASSERTIONS_APP);
SessionNavigationRequest<T> request = new SessionNavigationRequest<>(
mockMvc, responseClass, installReference);
return request.request(selections);
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.commcare.formplayer.tests;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.commcare.formplayer.application.MenuController;
import org.commcare.formplayer.beans.SessionNavigationBean;
import org.commcare.formplayer.beans.menus.BaseResponseBean;
import org.commcare.formplayer.beans.menus.CommandListResponseBean;
import org.commcare.formplayer.beans.menus.EntityBean;
import org.commcare.formplayer.beans.menus.EntityListResponse;
import org.commcare.formplayer.configuration.CacheConfiguration;
import org.commcare.formplayer.junit.InitializeStaticsExtension;
import org.commcare.formplayer.junit.Installer;
import org.commcare.formplayer.junit.RestoreFactoryExtension;
import org.commcare.formplayer.junit.StorageFactoryExtension;
import org.commcare.formplayer.junit.request.Response;
import org.commcare.formplayer.junit.request.SessionNavigationRequest;
import org.commcare.formplayer.services.RestoreFactory;
import org.commcare.formplayer.utils.MockRequestUtils;
import org.commcare.formplayer.utils.TestContext;
import org.commcare.formplayer.web.client.WebClient;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;

@WebMvcTest
@Import({MenuController.class})
@ContextConfiguration(classes = {TestContext.class, CacheConfiguration.class})
@ExtendWith(InitializeStaticsExtension.class)
public class MenuAssertionsTest {

private static final String ASSERTIONS_APP = "basic";
@Autowired
protected WebClient webClientMock;

@Autowired
RestoreFactory restoreFactoryMock;

@Autowired
private MockMvc mockMvc;

private MockRequestUtils mockRequest;

@RegisterExtension
static RestoreFactoryExtension restoreFactoryExt = new RestoreFactoryExtension.builder()
.withUser("user").withDomain("domain")
.withRestorePath("restores/basic.xml")
.build();

@RegisterExtension
static StorageFactoryExtension storageExt = new StorageFactoryExtension.builder()
.withUser("user").withDomain("domain").build();

@BeforeEach
public void setUp() throws Exception {
mockRequest = new MockRequestUtils(webClientMock, restoreFactoryMock);
}


@Test
public void testMenuAssertionsFailures() {
// Check assertion failing for project_role user property
try {
assertThrows(Exception.class, ()->{
navigate(new String[]{"0"},
CommandListResponseBean.class);
});
} catch (Exception e) {
assertTrue(e.getMessage().contains("User must have a project role configured."));
}
// Check assertion failing for fixture
try {
Response<CommandListResponseBean> response1 = navigate(new String[]{"1"},
CommandListResponseBean.class);
} catch (Exception e) {
assertTrue(e.getMessage().contains("This user must be in exactly one case sharing group."));
}
}


private <T extends BaseResponseBean> Response<T> navigate(
String[] selections, Class<T> responseClass) {
String installReference = Installer.getInstallReference(ASSERTIONS_APP);
SessionNavigationRequest<T> request = new SessionNavigationRequest<>(
mockMvc, responseClass, installReference);
return request.request(selections);
}
}

Binary file modified src/test/resources/archives/basic.ccz
Binary file not shown.
26 changes: 26 additions & 0 deletions src/test/resources/restores/project_role_admin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<OpenRosaResponse xmlns="http://openrosa.org/http/response">
<message nature="ota_restore_success">Successfully restored account vl1!</message>
<Sync xmlns="http://commcarehq.org/sync">
<restore_id>9f7c4e0a3a8ca4e2ecf54444fb7e70cf</restore_id>
</Sync>
<Registration xmlns="http://openrosa.org/user/registration">
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we trim the file to only contain what's necessary for the test. I believe you should be able to remove most of the info in this file for the assertion test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good point. Yes, I'll trim it down.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

restore updated here 0663863

<username>vl1</username>
<password>sha1$KOXD0OGyqKTP$139e6226da49dd500336dcd189b519da06aeb087</password>
<uuid>7afceb0259b2866be17b3632392f8a4b</uuid>
<date>2014-12-30</date>
<user_data>
<data key="commcare_project">ccqa</data>
<data key="commcare_last_name" />
<data key="commcare_phone_number" />
<data key="commcare_first_name" />
<data key="project_role">basic_user</data>
</user_data>b
</Registration>
<fixture id="user-groups" user_id="7afceb0259b2866be17b3632392f8a4b">
<groups>
<group id="06545890ff6536cf29136510f265cc7e">
<name>Test Case Sharing 2</name>
</group>
</groups>
</fixture>
</OpenRosaResponse>