Skip to content

Instantly share code, notes, and snippets.

@pcon
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcon/ca95d121d71ead05dedf to your computer and use it in GitHub Desktop.
Save pcon/ca95d121d71ead05dedf to your computer and use it in GitHub Desktop.
/**
* Gets an admin user
*
* @return An admin user
*/
public static User getAdminUser() {
Profile adminProfile = [
select id
from profile
where name = 'System Administrator'
];
User admin = [
select Name
from User
where ProfileId = :adminProfile.Id and
isActive = true and
LocaleSidKey = 'en_US'
limit 1
];
return admin;
}
/**
* Gets a user with the given profile and role
*
* @param username The username to use (must be in the form of an email)
* @param profile The name of the profile to use
* @param role The name of the role to use
* @return A test user
*/
public static User getTestUser(String username, String profile, String role) {
Profile currentProfile = [
select id
from profile
where name = :profile
];
Userrole currentRole = [
select id
from UserRole
where Name = :role
];
return new User(
alias = 'admintes',
email = 'noreply@test.com',
emailencodingkey = 'UTF-8',
lastname = 'Testing',
languagelocalekey = 'en_US',
localesidkey = 'en_US',
profileid = currentProfile.Id,
timezonesidkey = 'America/New_York',
username = username,
userroleid = currentRole.Id,
);
}
/**
* Creates a test user
*
* (NOTE: inserts user record)
* @param username The username to use (must be in the form of an email)
* @param profile The name of the profile to use
* @param role The name of the role to use
* @return A test user
*/
public static User createTestUser(String username, String profile, String role) {
User admin = getAdminUser();
User testUser;
System.runAs(admin) {
testUser = getTestUser(username, profile, role);
insert testUser;
}
return testUser;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment