github.com/FusionAuth/go-client@v0.0.0-20240425220342-2317e10dfcf5/pkg/fusionauth/Domain_test.go (about)

     1  /*
     2   * Copyright (c) 2020, FusionAuth, All Rights Reserved
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing,
    11   * software distributed under the License is distributed on an
    12   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
    13   * either express or implied. See the License for the specific
    14   * language governing permissions and limitations under the License.
    15   */
    16  package fusionauth
    17  
    18  import (
    19  	"github.com/stretchr/testify/assert"
    20  	"testing"
    21  )
    22  
    23  func TestErrorsPresentReturnsTrueWhenFieldErrorsExist(t *testing.T) {
    24  	errors := Errors{
    25  		FieldErrors: map[string][]Error{
    26  			"user.username": []Error{
    27  				Error{
    28  					Code:    "[duplicate.email]",
    29  					Message: "The email 'example@example.com' is already registered.",
    30  				},
    31  			},
    32  		},
    33  		GeneralErrors: []Error{},
    34  	}
    35  	assert.True(t, errors.Present())
    36  }
    37  
    38  func TestErrorsPresentReturnsTrueWhenGeneralErrorsExist(t *testing.T) {
    39  	errors := Errors{
    40  		GeneralErrors: []Error{
    41  			Error{
    42  				Code:    "",
    43  				Message: "Token has expired.",
    44  			},
    45  		},
    46  	}
    47  	assert.True(t, errors.Present())
    48  }
    49  
    50  func TestErrorsPresentReturnsTrueWhenBothFieldAndGeneralErrorsExist(t *testing.T) {
    51  	errors := Errors{
    52  		FieldErrors: map[string][]Error{
    53  			"user.username": []Error{
    54  				Error{
    55  					Code:    "[duplicate.email]",
    56  					Message: "The email 'example@example.com' is already registered.",
    57  				},
    58  			},
    59  		},
    60  		GeneralErrors: []Error{
    61  			Error{
    62  				Code:    "",
    63  				Message: "Token has expired.",
    64  			},
    65  		},
    66  	}
    67  	assert.True(t, errors.Present())
    68  }
    69  
    70  func TestErrorsPresentReturnsFalseWhenNeitherFieldAndGeneralErrorsExist(t *testing.T) {
    71  	errors := Errors{
    72  		FieldErrors:   map[string][]Error{},
    73  		GeneralErrors: []Error{},
    74  	}
    75  	assert.False(t, errors.Present())
    76  }
    77  
    78  func TestErrorsPrintedToReadableStringByDefault(t *testing.T) {
    79  	errors := Errors{
    80  		FieldErrors: map[string][]Error{
    81  			"user.username": []Error{
    82  				Error{
    83  					Code:    "[duplicate.email]",
    84  					Message: "The email 'example@example.com' is already registered.",
    85  				},
    86  			},
    87  		},
    88  		GeneralErrors: []Error{
    89  			Error{
    90  				Code:    "",
    91  				Message: "Token has expired.",
    92  			},
    93  		},
    94  	}
    95  
    96  	assert.Equal(t, "Token has expired. user.username: The email 'example@example.com' is already registered.", errors.Error())
    97  }