Provide Server-side service to support UI localization #265 (#322)

merging
This commit is contained in:
Nanne Baars
2017-01-31 17:52:33 +01:00
committed by misfir3
parent 355393352e
commit ee5a12d205
71 changed files with 875 additions and 926 deletions

View File

@ -1,31 +0,0 @@
package org.owasp.webgoat.plugins;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PluginTestHelper {
private static Path tempDirectory;
public static Path createTmpDir() throws IOException {
tempDirectory = Files.createTempDirectory(PluginTestHelper.class.getSimpleName());
tempDirectory.toFile().deleteOnExit();
return tempDirectory;
}
public static Path pathForLoading() throws IOException, URISyntaxException {
Path path = Paths.get(PluginTestHelper.class.getProtectionDomain().getCodeSource().getLocation().toURI());
return Paths.get(path.toString(), "org/owasp/webgoat/plugins");
}
// public static Plugin createPluginFor(Class pluginClass) throws Exception {
// Path pluginTargetPath = Files.createDirectory(Paths.get(tempDirectory.toString(), "pluginTargetPath"));
// Map<String, byte[]> classes = new HashMap<>();
// classes.put(pluginClass.getName(), Files.readAllBytes(Paths.get(pathForLoading().toString(), pluginClass.getSimpleName() + ".class")));
// Plugin plugin = new Plugin(pluginTargetPath, classes);
// return plugin;
// }
}

View File

@ -8,19 +8,14 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.owasp.webgoat.i18n.LabelManager;
import org.owasp.webgoat.lessons.AbstractLesson;
import org.owasp.webgoat.lessons.Assignment;
import org.owasp.webgoat.session.WebSession;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.junit.Assert.*;
import static org.mockito.AdditionalAnswers.returnsFirstArg;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
import static org.owasp.webgoat.service.HintService.URL_HINTS_MVC;
import static org.owasp.webgoat.service.LabelService.URL_LABELS_MVC;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

View File

@ -1,10 +1,9 @@
package org.owasp.webgoat.service;
import org.assertj.core.util.Maps;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.owasp.webgoat.i18n.LabelProvider;
import org.owasp.webgoat.session.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
@ -13,9 +12,6 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.util.Locale;
import static org.mockito.Mockito.when;
import static org.owasp.webgoat.service.LabelService.URL_LABELS_MVC;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -49,30 +45,28 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
* @version $Id: $Id
* @since November 29, 2016
*/
@WebMvcTest(value = {LabelService.class, LabelProvider.class})
@WebMvcTest(value = {LabelService.class})
@RunWith(SpringRunner.class)
public class LabelServiceTest {
@Autowired
public MockMvc mockMvc;
@MockBean
private LabelProvider labelProvider;
private Course course;
@Test
@WithMockUser(username = "guest", password = "guest")
public void withoutLocale() throws Exception {
when(labelProvider.getLabels(Locale.ENGLISH)).thenReturn(Maps.newHashMap("key", "value"));
mockMvc.perform(MockMvcRequestBuilders.get(URL_LABELS_MVC))
.andExpect(status().isOk())
.andExpect(jsonPath("key", CoreMatchers.is("value")));
.andExpect(jsonPath("password", CoreMatchers.is("Password")));
}
@Test
@WithMockUser(username = "guest", password = "guest")
public void withLocale() throws Exception {
when(labelProvider.getLabels(Locale.GERMAN)).thenReturn(Maps.newHashMap("key", "value"));
mockMvc.perform(MockMvcRequestBuilders.get(URL_LABELS_MVC).param("lang", "de"))
mockMvc.perform(MockMvcRequestBuilders.get(URL_LABELS_MVC).param("lang", "nl"))
.andExpect(status().isOk())
.andExpect(jsonPath("key", CoreMatchers.is("value")));
.andExpect(jsonPath("password", CoreMatchers.is("Wachtwoord")));
}
}

View File

@ -6,7 +6,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.owasp.webgoat.i18n.LabelManager;
import org.owasp.webgoat.lessons.AbstractLesson;
import org.owasp.webgoat.lessons.Assignment;
import org.owasp.webgoat.session.LessonTracker;
@ -65,9 +64,6 @@ public class LessonProgressServiceTest {
private LessonTracker lessonTracker;
@Mock
private WebSession websession;
@Mock
private LabelManager labelManager;
@Before
public void setup() {
@ -75,7 +71,7 @@ public class LessonProgressServiceTest {
when(userTracker.getLessonTracker(any())).thenReturn(lessonTracker);
when(websession.getCurrentLesson()).thenReturn(lesson);
when(lessonTracker.getLessonOverview()).thenReturn(Maps.newHashMap(assignment, true));
this.mockMvc = MockMvcBuilders.standaloneSetup(new LessonProgressService(labelManager, userTracker, websession)).build();
this.mockMvc = MockMvcBuilders.standaloneSetup(new LessonProgressService(userTracker, websession)).build();
}
@Test

View File

@ -1,34 +0,0 @@
package org.owasp.webgoat.util;
import org.hamcrest.CoreMatchers;
import org.junit.Test;
import org.owasp.webgoat.i18n.LabelProvider;
import java.util.Locale;
import static org.junit.Assert.assertThat;
public class LabelProviderTest {
@Test
public void defaultLabelsShouldBePresent() {
LabelProvider labelProvider = new LabelProvider();
assertThat(labelProvider.get(Locale.ENGLISH, "LessonCompleted"), CoreMatchers.equalTo(
"Congratulations. You have successfully completed this lesson."));
}
@Test
public void shouldFallBackToEnglishIfLanguageNotSupported() {
LabelProvider labelProvider = new LabelProvider();
assertThat(labelProvider.get(Locale.CHINESE, "LessonCompleted"), CoreMatchers.equalTo(
"Congratulations. You have successfully completed this lesson."));
}
@Test
public void shouldUseProvidedLanguageIfSupported() {
LabelProvider labelProvider = new LabelProvider();
assertThat(labelProvider.get(Locale.GERMAN, "RestartLesson"), CoreMatchers.equalTo(
"Lektion neu beginnen"));
}
}