How To Fix Unit Test Failures for LightningLoginFormControllerTest.testIsUsernamePasswordEnabled

Our release pipeline runs all unit test for validation and all of a sudden we are getting this error which failing the unit test. These are autogenerated codes by Salesforce so it was a head scratcher why this will suddenly fail.

LightningLoginFormControllerTest.testIsUsernamePasswordEnabled
message: System.AssertException: Assertion Failed: Expected: true, Actual: false stacktrace:
Class.LightningLoginFormControllerTest.testIsUsernamePasswordEnabled: line 12, column 1

There are 2 possible options to fix this.

Option 1 – Update the Apex Code

Edit the existing unit test as likely the Network.getNetworkID() method is now returning null even though there are active communities in your org. Here we try to get the first active network.

    @TestVisible
    private static Auth.AuthConfiguration getAuthConfig(){
        Id networkId = Network.getNetworkId();
        if (networkId == null) {
            // fallback: pick first Live network in org if appropriate
            Network n = [SELECT Id FROM Network WHERE Status = 'Live' LIMIT 1];
            if (n != null) networkId = n.Id;
        }
        if (networkId == null) return null;
        return new Auth.AuthConfiguration(networkId, '');
    }

Option 2 – Add Login Form as an Authentication Service

Add Login Form for Authentication Service on the Setup > My Domain, Authentication Configuration.

Hope you find this useful or share more information.

Leave a Reply

Your email address will not be published. Required fields are marked *