Skip to content

Commit

Permalink
test(controllers): password check, encryption and query build
Browse files Browse the repository at this point in the history
  • Loading branch information
tiaguinho committed Oct 20, 2020
1 parent 59e9029 commit 54298dd
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions controllers/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package controllers

import (
"crypto/md5"
"crypto/sha1"
"fmt"
"testing"

"github.com/prest/prest/adapters/postgres"
"github.com/prest/prest/config"
)

func Test_basicPasswordCheck(t *testing.T) {
config.Load()
postgres.Load()

_, err := basicPasswordCheck("test@postgres.rest", "123456")
if err != nil {
t.Errorf("expected authenticated user, got: %s", err)
}
}

func Test_getSelectQuery(t *testing.T) {
config.Load()

expected := "SELECT * FROM test_users WHERE email=$1 AND password=$2 LIMIT 1"
query := getSelectQuery()

if query != expected {
t.Errorf("expected query: %s, got: %s", expected, query)
}
}

func Test_encrypt(t *testing.T) {
config.Load()

pwd := "123456"
enc := encrypt(pwd)

md5Enc := fmt.Sprintf("%x", md5.Sum([]byte(pwd)))
if enc != md5Enc {
t.Errorf("expected encrypted password to be: %s, got: %s", enc, md5Enc)
}

config.PrestConf.AuthEncrypt = "SHA1"

enc = encrypt(pwd)

sha1Enc := fmt.Sprintf("%x", sha1.Sum([]byte(pwd)))
if enc != sha1Enc {
t.Errorf("expected encrypted password to be: %s, got: %s", enc, sha1Enc)
}
}

0 comments on commit 54298dd

Please sign in to comment.