Testing function that returns gin.HandlerFunc

16 Views Asked by At

I have a golang function that returns a gin.HandlerFunc:

func CheckLoggingServiceAccount(config *config) gin.HandlerFunc {
    return func(c *gin.Context) {
        userId, _ := c.Get("userID")
        log.Info().Msgf("CheckLoggingServiceAccount invoked with userId %v, user %s", userId, userId.(string))
        allowedServiceAccountList := config.ServiceAccountsLogging

        serviceAccounts := strings.Split(allowedServiceAccountList, ",")
    
        if !util.Contains(serviceAccounts, userId.(string)) {
            log.Error().Msg(fmt.Sprintf("user %s is not allowed to perform this operation", userId))
            model.RespForbidden(c, fmt.Errorf("user %s is not allowed to perform this operation", userId))
            c.Abort()
            return
        }
        c.Next()
        return
    }
}

I've tried different ways of testing this but nothing works. Does anyone have any suggestions? Thanks!

0

There are 0 best solutions below