diff --git a/authentication/sourceTest/java/ch/systemsx/cisd/authentication/file/FileAuthenticationServiceTest.java b/authentication/sourceTest/java/ch/systemsx/cisd/authentication/file/FileAuthenticationServiceTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f98c7bc3b33c58bb20b479b479991af81cadb0eb --- /dev/null +++ b/authentication/sourceTest/java/ch/systemsx/cisd/authentication/file/FileAuthenticationServiceTest.java @@ -0,0 +1,158 @@ +/* + * Copyright 2008 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.authentication.file; + +import static org.testng.AssertJUnit.*; + +import org.jmock.Expectations; +import org.jmock.Mockery; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +/** + * Test cases for the {@link FileAuthenticationService}. + * + * @author Bernd Rinn + */ +public class FileAuthenticationServiceTest +{ + + private Mockery context; + + private IUserStore userStore; + + private FileAuthenticationService authService; + + @BeforeMethod + public final void setUp() + { + context = new Mockery(); + userStore = context.mock(IUserStore.class); + context.checking(new Expectations() + { + { + one(userStore).check(); + } + }); + authService = new FileAuthenticationService(userStore); + } + + @AfterMethod + public void tearDown() + { + context.assertIsSatisfied(); + } + + @Test + public void testAuthenticateApplication() + { + final String token = "Some Token"; + context.checking(new Expectations() + { + { + one(userStore).getId(); + will(returnValue(token)); + } + }); + assertEquals(token, authService.authenticateApplication()); + context.assertIsSatisfied(); + } + + @Test + public void testAuthenticateUserSuccess() + { + final String token = "Some Token"; + final String user = "User"; + final String password = "passw0rd"; + context.checking(new Expectations() + { + { + one(userStore).getId(); + will(returnValue(token)); + one(userStore).isPasswordCorrect(user, password); + will(returnValue(true)); + } + }); + assertTrue(authService.authenticateUser(token, user, password)); + context.assertIsSatisfied(); + } + + @Test + public void testAuthenticateUserFailure() + { + final String token = "Some Token"; + final String user = "User"; + final String password = "passw0rd"; + context.checking(new Expectations() + { + { + one(userStore).getId(); + will(returnValue(token)); + one(userStore).isPasswordCorrect(user, password); + will(returnValue(false)); + } + }); + assertFalse(authService.authenticateUser(token, user, password)); + context.assertIsSatisfied(); + } + + @Test + public void testGetPrincipalSuccess() + { + final String token = "Some Token"; + final String uid = "uid"; + final UserEntry user = new UserEntry(uid, "email", "first", "last", "pwd"); + context.checking(new Expectations() + { + { + one(userStore).getId(); + will(returnValue(token)); + one(userStore).tryGetUser(uid); + will(returnValue(user)); + } + }); + assertEquals(user.asPrincial(), authService.getPrincipal(token, uid)); + context.assertIsSatisfied(); + } + + @Test + public void testGetPrincipalFailure() + { + final String token = "Some Token"; + final String uid = "uid"; + context.checking(new Expectations() + { + { + one(userStore).getId(); + will(returnValue(token)); + one(userStore).tryGetUser(uid); + will(returnValue(null)); + } + }); + try + { + authService.getPrincipal(token, uid); + fail("Unknown user went undetected."); + } catch (IllegalArgumentException ex) + { + assertEquals("Cannot find user 'uid'.", ex.getMessage()); + } + context.assertIsSatisfied(); + } + +} diff --git a/authentication/sourceTest/java/ch/systemsx/cisd/authentication/file/FileBasedLineStoreTest.java b/authentication/sourceTest/java/ch/systemsx/cisd/authentication/file/FileBasedLineStoreTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a0647c61453ccee1f53b86301b997b0caf689a77 --- /dev/null +++ b/authentication/sourceTest/java/ch/systemsx/cisd/authentication/file/FileBasedLineStoreTest.java @@ -0,0 +1,104 @@ +/* + * Copyright 2008 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.authentication.file; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import static org.testng.AssertJUnit.*; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.lang.StringUtils; +import org.testng.annotations.Test; + +/** + * Test cases for the {@link FileBasedLineStore}. + * + * @author Bernd Rinn + */ +public class FileBasedLineStoreTest +{ + + private final static String fileName = "store"; + + private final static String fileDescription = "Some sort of file"; + + private static final File unitTestRootDirectory = + new File("targets" + File.separator + "unit-test-wd"); + + private static final File workingDirectory = + new File(unitTestRootDirectory, "FileBasedLineStoreTest"); + + private final static File file = new File(workingDirectory, fileName); + + private final static FileBasedLineStore store = new FileBasedLineStore(file, fileDescription); + + @Test + public void testCheck() + { + file.delete(); + assertFalse(file.exists()); + store.check(); + assertTrue(file.exists()); + } + + @Test + public void testGetId() + { + assertEquals(file.getPath(), store.getId()); + } + + @Test + public void testReadLinesNonExisting() + { + file.delete(); + assertFalse(file.exists()); + assertEquals(0, store.readLines().size()); + assertFalse(file.exists()); + } + + @Test + public void testRoundtrip() throws IOException + { + final File svFile = new File(file.getPath() + ".sv"); + svFile.delete(); + assertFalse(svFile.exists()); + file.delete(); + assertFalse(file.exists()); + + final List<String> lines1 = Arrays.asList("1", "2", "3"); + store.writeLines(lines1); + assertTrue(file.exists()); + assertEquals(StringUtils.join(lines1, '\n') + "\n", FileUtils.readFileToString(file)); + final List<String> linesRead1 = store.readLines(); + assertEquals(lines1, linesRead1); + assertTrue(svFile.exists()); + assertEquals(0, svFile.length()); + + final List<String> lines2 = Arrays.asList("4", "5", "6"); + store.writeLines(lines2); + assertTrue(file.exists()); + assertTrue(svFile.exists()); + assertEquals(StringUtils.join(lines1, '\n') + "\n", FileUtils.readFileToString(svFile)); + assertEquals(StringUtils.join(lines2, '\n') + "\n", FileUtils.readFileToString(file)); + final List<String> linesRead2 = store.readLines(); + assertEquals(lines2, linesRead2); + } + +}