Skip to content

Instantly share code, notes, and snippets.

@cmoulliard
Last active August 29, 2015 14:22
Show Gist options
  • Save cmoulliard/be7192152523b85dd852 to your computer and use it in GitHub Desktop.
Save cmoulliard/be7192152523b85dd852 to your computer and use it in GitHub Desktop.
public synchronized TestAccount create(String username, String email,
String fullName, String... groups)
throws OrmException, UnsupportedEncodingException, JSchException {
TestAccount account = accounts.get(username);
if (account != null) {
return account;
}
ReviewDb db = reviewDbProvider.open();
try {
Account.Id id = new Account.Id(db.nextAccountId());
KeyPair sshKey = genSshKey();
AccountSshKey key =
new AccountSshKey(new AccountSshKey.Id(id, 1), publicKey(sshKey, email));
AccountExternalId extUser =
new AccountExternalId(id, new AccountExternalId.Key(
AccountExternalId.SCHEME_USERNAME, username));
String httpPass = "http-pass";
extUser.setPassword(httpPass);
db.accountExternalIds().insert(Collections.singleton(extUser));
if (email != null) {
AccountExternalId extMailto = new AccountExternalId(id, getEmailKey(email));
extMailto.setEmailAddress(email);
db.accountExternalIds().insert(Collections.singleton(extMailto));
}
Account a = new Account(id, TimeUtil.nowTs());
a.setFullName(fullName);
a.setPreferredEmail(email);
db.accounts().insert(Collections.singleton(a));
db.accountSshKeys().insert(Collections.singleton(key));
if (groups != null) {
for (String n : groups) {
AccountGroup.NameKey k = new AccountGroup.NameKey(n);
AccountGroup g = groupCache.get(k);
AccountGroupMember m =
new AccountGroupMember(new AccountGroupMember.Key(id, g.getId()));
db.accountGroupMembers().insert(Collections.singleton(m));
}
}
sshKeyCache.evict(username);
accountCache.evictByUsername(username);
byEmailCache.evict(email);
account =
new TestAccount(id, username, email, fullName, sshKey, httpPass);
accounts.put(username, account);
return account;
} finally {
db.close();
}
}
private static KeyPair genSshKey() throws JSchException {
JSch jsch = new JSch();
return KeyPair.genKeyPair(jsch, KeyPair.RSA);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment