Passed
Push — main ( 7e619e...36c76a )
by Yume
01:54
created

pkg/crypto/crypto_test.go   A

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 28
dl 0
loc 50
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A crypto_test.MockCrypto.Verify 0 3 1
A crypto_test.TestCrypto_Hash 0 12 3
A crypto_test.TestCrypto_Verify 0 14 3
A crypto_test.MockCrypto.Hash 0 3 1
1
package crypto_test
2
3
import (
4
	"testing"
5
6
	"github.com/memnix/memnix-rest/pkg/crypto"
7
)
8
9
// MockCrypto is a mock implementation of the ICrypto interface for testing.
10
type MockCrypto struct{}
11
12
func (mc MockCrypto) Hash(_ string) ([]byte, error) {
13
	// Implement a mock hashing function for testing
14
	return []byte("mocked-hash"), nil
15
}
16
17
func (mc MockCrypto) Verify(_ string, hash []byte) (bool, error) {
18
	// Implement a mock verify function for testing
19
	return string(hash) == "mocked-hash", nil
20
}
21
22
func TestCrypto_Hash(t *testing.T) {
23
	mockCrypto := MockCrypto{}
24
	cryptoHelper := crypto.Crypto{Crypto: mockCrypto}
25
26
	password := "my_password"
27
	hashedPassword, err := cryptoHelper.Hash(password)
28
	if err != nil {
29
		t.Errorf("Hash() returned an error: %v", err)
30
	}
31
32
	if string(hashedPassword) != "mocked-hash" {
33
		t.Errorf("Hash() did not return the expected hashed value")
34
	}
35
}
36
37
func TestCrypto_Verify(t *testing.T) {
38
	mockCrypto := MockCrypto{}
39
	cryptoHelper := crypto.Crypto{Crypto: mockCrypto}
40
41
	password := "my_password"
42
	hashedPassword, _ := cryptoHelper.Hash(password)
43
44
	match, err := cryptoHelper.Verify(password, hashedPassword)
45
	if err != nil {
46
		t.Errorf("Verify() returned an error: %v", err)
47
	}
48
49
	if !match {
50
		t.Errorf("Verify() did not return a match for the hashed password")
51
	}
52
}
53