private static ProtoHumanAccountData serialize(HumanAccountData account) { Preconditions.checkNotNull(account, "account is null"); ProtoHumanAccountData.Builder builder = ProtoHumanAccountData.newBuilder(); if (account.getPasswordDigest() != null) { builder.setPasswordDigest(serialize(account.getPasswordDigest())); } return builder.build(); }
String locale = account.asHuman().getLocale(); if (locale != null) { String requestLocale = UrlParameters.getParameters(request.getQueryString()).get("locale");
protected static HumanAccountData createHumanAccount(ParticipantId pid, String email, String avatarFileId, String locale) { HumanAccountData account = new HumanAccountDataImpl(pid); account.setAvatarFileId(avatarFileId); account.setEmail(email); account.setLocale(locale); return account; }
public void testGetAccount() throws InvalidParticipantAddress, PersistenceException, IOException { // Mock data accountStore.putAccount(ACCOUNT_JOE); when(sessionManager.getLoggedInUser((HttpSession) anyObject())).thenReturn(ACCOUNT_JOE.getId()); when(sessionManager.getLoggedInUser((HttpServletRequest) anyObject())).thenReturn( ACCOUNT_JOE.getId()); // Test AccountServiceData requestData = new AccountServiceData(); ByteArrayOutputStream responseStream = new ByteArrayOutputStream(); executeService("/account/joe", "GET", requestData, responseStream, HttpServletResponse.SC_OK); AccountServiceData responseData = (AccountServiceData) ServiceData.fromJson(responseStream.toString("UTF-8"), AccountServiceData.class); assertEquals(ACCOUNT_JOE.getEmail(), responseData.email); assertEquals(ACCOUNT_JOE.getLocale(), responseData.locale); assertTrue(responseData.avatarUrl.contains("image.png")); }
public void testGetAccountNotLoggedUser() throws InvalidParticipantAddress, PersistenceException, IOException { // Mock data accountStore.putAccount(ACCOUNT_JOE); when(sessionManager.getLoggedInUser((HttpSession) anyObject())).thenReturn(ACCOUNT_MAT.getId()); when(sessionManager.getLoggedInUser((HttpServletRequest) anyObject())).thenReturn( ACCOUNT_MAT.getId()); // Test AccountServiceData requestData = new AccountServiceData(); ByteArrayOutputStream responseStream = new ByteArrayOutputStream(); executeService("/account/joe", "GET", requestData, responseStream, HttpServletResponse.SC_OK); AccountServiceData responseData = (AccountServiceData) ServiceData.fromJson(responseStream.toString("UTF-8"), AccountServiceData.class); assertNull(responseData.email); assertEquals(ACCOUNT_JOE.getLocale(), responseData.locale); assertTrue(responseData.avatarUrl.contains("image.png")); }
protected void assertEqualsAccount(HumanAccountData expected, AccountServiceData actual) { assertEquals(expected.getId().getAddress(), actual.id); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { try { ParticipantId participant = sessionManager.getLoggedInUser(req.getSession(false)); if (participant == null) { resp.setStatus(HttpServletResponse.SC_FORBIDDEN); return; } String locale = URLDecoder.decode(req.getParameter("locale"), "UTF-8"); AccountData account = accountStore.getAccount(participant); HumanAccountData humanAccount; if (account != null) { humanAccount = account.asHuman(); } else { humanAccount = new HumanAccountDataImpl(participant); } humanAccount.setLocale(locale); accountStore.putAccount(humanAccount); } catch (PersistenceException ex) { throw new IOException(ex); } } }
public void testUpdateAccount() throws PersistenceException, InvalidParticipantAddress, IOException { // Mock data accountStore.putAccount(ACCOUNT_JOE); when(sessionManager.getLoggedInUser((HttpSession) anyObject())).thenReturn(ACCOUNT_JOE.getId()); when(sessionManager.getLoggedInUser((HttpServletRequest) anyObject())).thenReturn( ACCOUNT_JOE.getId()); // Test AccountServiceData requestData = new AccountServiceData(); requestData.email = "joe@email.example.com"; requestData.locale = "es_ES"; requestData.password = "_password_"; requestData.avatarData = IMAGE_BASE64; ByteArrayOutputStream responseStream = new ByteArrayOutputStream(); executeService("/account/joe", "POST", requestData, responseStream, HttpServletResponse.SC_OK); AccountServiceData responseData = (AccountServiceData) ServiceData.fromJson(responseStream.toString("UTF-8"), AccountServiceData.class); assertEquals(requestData.email, responseData.email); assertEquals(requestData.locale, responseData.locale); assertTrue(responseData.avatarUrl.contains("image.png")); }
private DBObject humanToObject(HumanAccountData account) { DBObject object = new BasicDBObject(); PasswordDigest digest = account.getPasswordDigest(); if (digest != null) { DBObject digestObj = new BasicDBObject(); digestObj.put(PASSWORD_SALT_FIELD, digest.getSalt()); digestObj.put(PASSWORD_DIGEST_FIELD, digest.getDigest()); object.put(HUMAN_PASSWORD_FIELD, digestObj); } return object; }
public void testQueryAccounts() throws PersistenceException, IOException { // Mock data accountStore.putAccount(ACCOUNT_JOE); accountStore.putAccount(ACCOUNT_TOM); accountStore.putAccount(ACCOUNT_MAT); when(sessionManager.getLoggedInUser((HttpSession) anyObject())).thenReturn(ACCOUNT_MAT.getId()); when(sessionManager.getLoggedInUser((HttpServletRequest) anyObject())).thenReturn( ACCOUNT_MAT.getId()); when(sessionManager.listLoggedInUsers((HttpServletRequest) anyObject())).thenReturn(CollectionUtils.immutableSet(ACCOUNT_MAT.getId())); // Test AccountServiceData requestData = new AccountServiceData(); ByteArrayOutputStream responseStream = new ByteArrayOutputStream(); executeService("/account", "GET", requestData, responseStream, HttpServletResponse.SC_OK, ImmutableMap.<String, String> of("p", "joe@example.com;tom@example.com;mike@example.com;mat@example.com")); AccountServiceData[] responseData = (AccountServiceData[]) ServiceData.arrayFromJson(responseStream.toString("UTF-8"), AccountServiceData[].class); assertEquals(4, responseData.length); // We expect results in the same order as query assertEqualsAccount(ACCOUNT_JOE, responseData[0]); assertEqualsAccount(ACCOUNT_TOM, responseData[1]); assertEqualsAccount(ACCOUNT_MIKE, responseData[2]); assertEqualsAccount(ACCOUNT_MAT, responseData[3]); }
} else if (!account.asHuman().getPasswordDigest().verify(password)) {
public void testPasswordDigestVerifies() { HumanAccountData account = new HumanAccountDataImpl(ParticipantId.ofUnsafe("captainhammer@example.com"), new PasswordDigest("wonderflownium".toCharArray())); assertNotNull(account.getPasswordDigest()); assertTrue(account.getPasswordDigest().verify("wonderflownium".toCharArray())); }
public void testPasswordDigestVerifies() { HumanAccountData account = new HumanAccountDataImpl(ParticipantId.ofUnsafe("captainhammer@example.com"), new PasswordDigest("wonderflownium".toCharArray())); assertNotNull(account.getPasswordDigest()); assertTrue(account.getPasswordDigest().verify("wonderflownium".toCharArray())); }
public void testRegisterNewUserEnabled() throws Exception { attemptToRegister(req, resp, "foo@example.com", "internet", false); verify(resp).setStatus(HttpServletResponse.SC_OK); ParticipantId participantId = ParticipantId.ofUnsafe("foo@example.com"); AccountData account = store.getAccount(participantId); assertNotNull(account); assertTrue(account.asHuman().getPasswordDigest().verify("internet".toCharArray())); verify(welcomeBot).greet(eq(participantId)); }
public void testUserWithNoPasswordHasNoPasswordDigest() { HumanAccountData account = new HumanAccountDataImpl(ParticipantId.ofUnsafe("moist@example.com")); assertNull(account.getPasswordDigest()); } }
public void testUserWithNoPasswordHasNoPasswordDigest() { HumanAccountData account = new HumanAccountDataImpl(ParticipantId.ofUnsafe("moist@example.com")); assertNull(account.getPasswordDigest()); } }
public void testRegisterNewUserEnabled() throws Exception { attemptToRegister(req, resp, "foo@example.com", "internet", false); verify(resp).setStatus(HttpServletResponse.SC_OK); ParticipantId participantId = ParticipantId.ofUnsafe("foo@example.com"); AccountData account = store.getAccount(participantId); assertNotNull(account); assertTrue(account.asHuman().getPasswordDigest().verify("internet".toCharArray())); verify(welcomeBot).greet(eq(participantId)); }
public void testNullPasswordWorks() throws Exception { attemptToRegister(req, resp, "zd@example.com", null, false); verify(resp).setStatus(HttpServletResponse.SC_OK); AccountData account = store.getAccount(ParticipantId.ofUnsafe("zd@example.com")); assertNotNull(account); assertTrue(account.asHuman().getPasswordDigest().verify("".toCharArray())); }