Verification emails

This commit is contained in:
2024-03-21 21:10:56 +01:00
parent 782e762019
commit b9a1fcd02a
9 changed files with 292 additions and 79 deletions

View File

@ -7,7 +7,6 @@ func HashPassword(password string, cost int) (string, error) {
return string(bytes), err
}
func CheckPasswordHash(password, hash string) bool {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err == nil
func CheckPasswordHash(password, hash string) error {
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
}

View File

@ -11,11 +11,11 @@ func TestHashValid(t *testing.T) {
password := "qwertyu9"
hpass, err := hash.HashPassword(password, 10)
assert.NoError(t, err)
assert.True(t, hash.CheckPasswordHash(password, hpass))
assert.NoError(t, hash.CheckPasswordHash(password, hpass))
}
func TestHashInvalid(t *testing.T) {
password := "qwertyu9"
invhash := "qwertyu9"
assert.False(t, hash.CheckPasswordHash(password, invhash))
assert.Error(t, hash.CheckPasswordHash(password, invhash))
}

View File

@ -0,0 +1,55 @@
package kube
import (
"context"
"fmt"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func Create(ctx context.Context, client client.Client, obj client.Object, wait bool) error {
if err := client.Create(ctx, obj); err != nil {
return err
}
if wait{
if err := WaitUntilCreated(ctx, client, obj, 10, time.Millisecond*50); err != nil {
return err
}
}
return nil
}
func SetOwnerRef(ctx context.Context, client client.Client, obj client.Object, owner client.Object) client.Object {
apiVersion := fmt.Sprintf("%s/%s", owner.GetObjectKind().GroupVersionKind().Group, owner.GetObjectKind().GroupVersionKind().Version)
ownerReference := []metav1.OwnerReference{
{
APIVersion: apiVersion,
Kind: owner.GetObjectKind().GroupVersionKind().GroupKind().Kind,
Name: owner.GetName(),
UID: owner.GetUID(),
},
}
obj.SetOwnerReferences(ownerReference)
return obj
}
func WaitUntilCreated(ctx context.Context, client client.Client, obj client.Object, attemps int, timeout time.Duration) error {
if err := client.Get(ctx, types.NamespacedName{
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
}, obj); err != nil {
if attemps > 0 {
time.Sleep(timeout)
if err := WaitUntilCreated(ctx, client, obj, attemps-1, timeout); err != nil {
return err
}
} else {
return err
}
}
return nil
}

View File

@ -0,0 +1,12 @@
package kube_test
import (
"testing"
"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube"
"github.com/alecthomas/assert/v2"
)