utils

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2023 License: MIT Imports: 23 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertResponse added in v1.2.2

func AssertResponse(expectedStatusCode int, resp *http.Response, responseBody []byte) error

AssertResponse asserts the response status code and body.

func AuthenticateOIDCToken added in v1.1.8

func AuthenticateOIDCToken(w http.ResponseWriter, r *http.Request, authTokenKey string, expectedAudience, expectedIssuer string) (*types.AuthResponse, error)

AuthenticateOIDCToken verifies a JWT token's authenticity based on the provided audience and issuer. It returns a success response if the token is valid. It returns an error if the token is invalid or does not match the expected audience and issuer.

func CheckErrorAndFail added in v1.2.4

func CheckErrorAndFail(t *testing.T, err error)

CheckErrorAndFail checks if an error is not nil and fails the test if it is. To be utilized with test level (global) error field.

func CleanupMock added in v1.2.2

func CleanupMock(ctx context.Context, app *firebase.App, user *auth.UserRecord) error

CleanupMock deletes the Firebase user. Typically utilized in integration tests to clean up the user after the test is complete. Use "defer" to call this function after the test is complete to delete the mock user from Firebase.

func CreateEmptyPool added in v1.1.0

func CreateEmptyPool(maxWorkers int) *pool

CreateEmptyPool creates a new worker pool with the specified maximum number of workers. It returns a pointer to the created Pool.

func CreateFirebaseUser added in v1.2.2

func CreateFirebaseUser(ctx context.Context, app *firebase.App, httpClient *http.Client, user *auth.UserToCreate, email string) (*auth.UserRecord, error)

CreateFirebaseUser creates a Firebase user and returns the user record. An embedded filesystem and the path to the credentials file are required. User is created with the specified email and password only. The user to be passed to the function must be a pointer to a struct of type auth.UserToCreate. user := auth.UserToCreate{} user.Email(mockEmail) user.Password(mockPwd)

func CreateHTTPRequestNoBearer added in v1.2.2

func CreateHTTPRequestNoBearer(method, url, customTokenKey, idToken string, body io.Reader) (*http.Request, error)

CreateHTTPRequestNoBearer creates an HTTP request with the given method, URL, idToken and body. Pass the auth token as a custom header using your own key and token value.

func CreateHTTPRequestWithBearer added in v1.2.2

func CreateHTTPRequestWithBearer(method, url, customTokenKey, idToken string, body io.Reader) (*http.Request, error)

CreateHTTPRequest creates an HTTP request with the given method, URL, idToken and body. Pass the auth token as a custom header using your own key and token value. The token value will be prepended with "Bearer ".

func CreateOIDCToken added in v1.1.8

func CreateOIDCToken(FS embed.FS, credentialsPath string, audience string) (*oauth2.Token, error)

CreateOIDCToken creates an OIDC token with the given audience.

func GetComponent

func GetComponent() string

GetComponent returns details about the caller function. It is utilized to populate the component field of a GoogleLogEntry, or any other log entry component field.

func GetFirebaseIDToken

func GetFirebaseIDToken(email, password, firebaseAPIKey string) (string, error)

GetFirebaseIDToken exchanges an email and password of an active Firebase user for its Firebase UID. The email and password are used to authenticate with Firebase and represent the user's credentials. A valid Firebase API key is required to authenticate with the Firebase API. The Firebase ID token is returned. If the request fails, an error is returned.

func GetSecretValue added in v1.0.8

func GetSecretValue(FS embed.FS, credentialsPath string, projectID string, secretID string, versionID string) ([]byte, error)

GetSecretValue is a function to retrieve a secret value from Google Cloud Secret Manager. FS is the embed.FS embedded filesystem to pass to the function. The credentialsPath parameter is the path to the credentials file. The projectID parameter is the ID of the Google Cloud project. The secretID parameter is the ID of the secret. The versionID parameter is the ID of the secret version. The secret value is returned as a byte slice.

func InitFirebaseApp added in v1.2.2

func InitFirebaseApp(ctx context.Context, FS embed.FS, accountCredentialsPath string) (*firebase.App, error)

InitFirebaseApp initializes the Firebase Admin SDK and returns the app instance.

func NewNonNilValidator added in v1.2.9

func NewNonNilValidator(tagValue string) types.Validator

NewNonNilValidator creates a new validator that checks if a field is non-nil.

func NewNonZeroValidator added in v1.2.9

func NewNonZeroValidator(tagValue string) types.Validator

NewNonZeroValidator creates a new validator that checks if a field is non-zero.

func ReplaceNilPointersFields added in v1.2.6

func ReplaceNilPointersFields(toUpdate, currentDocument interface{}) error

ReplaceNilFields is a function designed to accept two pointers to structs of the same type. It will replace any nil POINTER fields in the first struct with the values from the second struct. This is useful for updating a struct in a database without overwriting the fields with nil values. Use this function to perform PATCH updates of a document in a database.

func WriteErrorResponse added in v1.0.7

func WriteErrorResponse(w http.ResponseWriter, msg string, errorCode int)

WriteErrorResponse writes an error response to the response writer with appropriate headers. It does NOT return an error because it is used to write an error response. Instead, if an error occurs internally, it writes the error to the response writer with http.Error.

func WriteGzipJSONResponse

func WriteGzipJSONResponse(w http.ResponseWriter, data interface{}, code int) error

WriteGzippedJSONResponse compresses the JSON data and writes it to the response writer with appropriate headers. The Content-Encoding header is set to gzip and the Content-Type header is set to application/json. The Content-Length header is set with the length of the compressed data. The status code is set based on the provided code parameter. If the data cannot be serialized to JSON, an error is returned. If the compressed data cannot be written to the response writer, an error is returned.

Types

type ValidatorRegistry added in v1.3.0

type ValidatorRegistry struct {
	Validators map[string]types.Validator
}

ValidatorRegistry is a struct that holds a map of validators.

func NewValidatorRegistry added in v1.3.0

func NewValidatorRegistry() *ValidatorRegistry

NewValidatorRegistry creates a new ValidatorRegistry and initializes its validators map.

func (*ValidatorRegistry) AddValidator added in v1.3.0

func (r *ValidatorRegistry) AddValidator(tag string, validator types.Validator)

AddValidator adds a new validator to the ValidatorRegistry.

func (*ValidatorRegistry) CheckTags added in v1.3.0

func (r *ValidatorRegistry) CheckTags(data interface{}, tagKey string) error

CheckTags checks the tags of the fields in a struct. If a field has a tag that matches a validator in the ValidatorRegistry, it will run that validator on the field.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL