• Jessie Rymph 12
  • NEWBIE
  • 10 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
@isTest
private class SiteGuestUserTest {
  @TestSetup
  static void testSetup(){
    	Account a = TestFactory.getAccount('boots factory', true);
      	contact c = TestFactory.getContact(a.id,'Connie','Connors', true, 'con@con.com','purchasing');
    
//set up a user with the profile for my site guest user. I read that I don't need to insert the user.
    UserRole userRole = new UserRole(DeveloperName = 'TestingTeam', Name = 'Testing Team');
    String profileName ='Unsubscribe Profile';
    User u = new User(
      ProfileId = [SELECT Id FROM Profile WHERE Name = :profileName].Id,
      LastName = 'last',
      Email = 'Cpt.Awesome@awesomesauce.com',
      Username = 'Cpt.Awesome@awesomesauce.com',
      Profile WHERE Name = 'Guest'] WHERE ID = :ProfileId].id,
      CompanyName = 'Testing Co',
      Title = 'Captain',
      Alias = 'alias',
      TimeZoneSidKey = 'America/Los_Angeles',
      EmailEncodingKey = 'UTF-8',
      LanguageLocaleKey = 'en_US',
      LocaleSidKey = 'en_US'
     
    );

        System.runAs(u) {
              // The following code runs as user 'u'
              System.debug('Current User: ' + UserInfo.getUserName());
              System.debug('Current Profile: ' + UserInfo.getProfileId());
          }
    }
    
    @isTest static void  testPermissionPositive() {
//this test succeeds. the user can see the email address. 
        user u = [SELECT Id from User WHERE LastName = 'last'];
        system.runAs(u){
            Test.startTest();
            Contact connie = [SELECT Id, Email FROM CONTACT WHERE FirstName = 'Connie'];
            Test.stopTest();
            system.assertEquals('con@con.com',connie.Email,'Site Guest User can see the email');
            system.debug(connie.id + connie.Email);
        }
    } 
   //test fails. Site Guest User should not be able to see the department field. 
    @isTest static void  testPermissionNegative() {
        user u = [SELECT Id from User WHERE LastName = 'last'];
        system.runAs(u){
            Test.startTest();
            Contact connie = [SELECT Id, Email, Department FROM CONTACT WHERE FirstName = 'Connie'];
            Test.stopTest();
            system.assertEquals('',connie.Department,'Site Guest User cannot see the department');
            system.debug(connie.id + connie.Department);
        }
    }

I have a site guest user with "Unsubscribe Profile" and I would like to run a test to make sure that it's properly working. The user with this profile should only be able to see the fields Id and Email on the Contact. I believe the second test demonstrates that the guest user is able to see the department field, which they shouldn't. 

Do I need to establish that my user is license type of Guest? Is my premise correct that I can even test this way? What am I missing?
I am creating a package with a Flow that updates opportunities. The package automatically includes historical trending on the opportunity object as a dependency. That requires any org that installs my package to have historical trending turned on. I don't want this. I turned off historical trend reporting on opportunity, but I can't get that dependency to go away. entity-type feature dependency
@isTest
private class SiteGuestUserTest {
  @TestSetup
  static void testSetup(){
    	Account a = TestFactory.getAccount('boots factory', true);
      	contact c = TestFactory.getContact(a.id,'Connie','Connors', true, 'con@con.com','purchasing');
    
//set up a user with the profile for my site guest user. I read that I don't need to insert the user.
    UserRole userRole = new UserRole(DeveloperName = 'TestingTeam', Name = 'Testing Team');
    String profileName ='Unsubscribe Profile';
    User u = new User(
      ProfileId = [SELECT Id FROM Profile WHERE Name = :profileName].Id,
      LastName = 'last',
      Email = 'Cpt.Awesome@awesomesauce.com',
      Username = 'Cpt.Awesome@awesomesauce.com',
      Profile WHERE Name = 'Guest'] WHERE ID = :ProfileId].id,
      CompanyName = 'Testing Co',
      Title = 'Captain',
      Alias = 'alias',
      TimeZoneSidKey = 'America/Los_Angeles',
      EmailEncodingKey = 'UTF-8',
      LanguageLocaleKey = 'en_US',
      LocaleSidKey = 'en_US'
     
    );

        System.runAs(u) {
              // The following code runs as user 'u'
              System.debug('Current User: ' + UserInfo.getUserName());
              System.debug('Current Profile: ' + UserInfo.getProfileId());
          }
    }
    
    @isTest static void  testPermissionPositive() {
//this test succeeds. the user can see the email address. 
        user u = [SELECT Id from User WHERE LastName = 'last'];
        system.runAs(u){
            Test.startTest();
            Contact connie = [SELECT Id, Email FROM CONTACT WHERE FirstName = 'Connie'];
            Test.stopTest();
            system.assertEquals('con@con.com',connie.Email,'Site Guest User can see the email');
            system.debug(connie.id + connie.Email);
        }
    } 
   //test fails. Site Guest User should not be able to see the department field. 
    @isTest static void  testPermissionNegative() {
        user u = [SELECT Id from User WHERE LastName = 'last'];
        system.runAs(u){
            Test.startTest();
            Contact connie = [SELECT Id, Email, Department FROM CONTACT WHERE FirstName = 'Connie'];
            Test.stopTest();
            system.assertEquals('',connie.Department,'Site Guest User cannot see the department');
            system.debug(connie.id + connie.Department);
        }
    }

I have a site guest user with "Unsubscribe Profile" and I would like to run a test to make sure that it's properly working. The user with this profile should only be able to see the fields Id and Email on the Contact. I believe the second test demonstrates that the guest user is able to see the department field, which they shouldn't. 

Do I need to establish that my user is license type of Guest? Is my premise correct that I can even test this way? What am I missing?
@isTest
private class SiteGuestUserTest {
  @TestSetup
  static void testSetup(){
    	Account a = TestFactory.getAccount('boots factory', true);
      	contact c = TestFactory.getContact(a.id,'Connie','Connors', true, 'con@con.com','purchasing');
    
//set up a user with the profile for my site guest user. I read that I don't need to insert the user.
    UserRole userRole = new UserRole(DeveloperName = 'TestingTeam', Name = 'Testing Team');
    String profileName ='Unsubscribe Profile';
    User u = new User(
      ProfileId = [SELECT Id FROM Profile WHERE Name = :profileName].Id,
      LastName = 'last',
      Email = 'Cpt.Awesome@awesomesauce.com',
      Username = 'Cpt.Awesome@awesomesauce.com',
      Profile WHERE Name = 'Guest'] WHERE ID = :ProfileId].id,
      CompanyName = 'Testing Co',
      Title = 'Captain',
      Alias = 'alias',
      TimeZoneSidKey = 'America/Los_Angeles',
      EmailEncodingKey = 'UTF-8',
      LanguageLocaleKey = 'en_US',
      LocaleSidKey = 'en_US'
     
    );

        System.runAs(u) {
              // The following code runs as user 'u'
              System.debug('Current User: ' + UserInfo.getUserName());
              System.debug('Current Profile: ' + UserInfo.getProfileId());
          }
    }
    
    @isTest static void  testPermissionPositive() {
//this test succeeds. the user can see the email address. 
        user u = [SELECT Id from User WHERE LastName = 'last'];
        system.runAs(u){
            Test.startTest();
            Contact connie = [SELECT Id, Email FROM CONTACT WHERE FirstName = 'Connie'];
            Test.stopTest();
            system.assertEquals('con@con.com',connie.Email,'Site Guest User can see the email');
            system.debug(connie.id + connie.Email);
        }
    } 
   //test fails. Site Guest User should not be able to see the department field. 
    @isTest static void  testPermissionNegative() {
        user u = [SELECT Id from User WHERE LastName = 'last'];
        system.runAs(u){
            Test.startTest();
            Contact connie = [SELECT Id, Email, Department FROM CONTACT WHERE FirstName = 'Connie'];
            Test.stopTest();
            system.assertEquals('',connie.Department,'Site Guest User cannot see the department');
            system.debug(connie.id + connie.Department);
        }
    }

I have a site guest user with "Unsubscribe Profile" and I would like to run a test to make sure that it's properly working. The user with this profile should only be able to see the fields Id and Email on the Contact. I believe the second test demonstrates that the guest user is able to see the department field, which they shouldn't. 

Do I need to establish that my user is license type of Guest? Is my premise correct that I can even test this way? What am I missing?