Completed
Push — main ( 523495...f20dc9 )
by Yume
25s queued 13s
created

pkg/crypto/crypto_test.go   A

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 27
dl 0
loc 48
rs 10
c 0
b 0
f 0

4 Methods

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