-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbdd_userstore_test.go
More file actions
159 lines (135 loc) · 4.39 KB
/
bdd_userstore_test.go
File metadata and controls
159 lines (135 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package auth
import (
"context"
"fmt"
"github.com/cucumber/godog"
)
// User store and authentication BDD step implementations
func (ctx *AuthBDDTestContext) iHaveAUserStoreConfigured() error {
// User store is configured as part of the module
return nil
}
func (ctx *AuthBDDTestContext) iCreateANewUser() error {
user := &User{
ID: "new-user-123",
Email: "newuser@example.com",
}
err := ctx.service.userStore.CreateUser(context.Background(), user)
if err != nil {
ctx.lastError = err
return nil
}
ctx.user = user
ctx.userID = user.ID
return nil
}
func (ctx *AuthBDDTestContext) theUserShouldBeStoredSuccessfully() error {
if ctx.lastError != nil {
return fmt.Errorf("user creation failed: %v", ctx.lastError)
}
return nil
}
func (ctx *AuthBDDTestContext) iShouldBeAbleToRetrieveTheUserByID() error {
user, err := ctx.service.userStore.GetUser(context.Background(), ctx.userID)
if err != nil {
return fmt.Errorf("failed to retrieve user: %v", err)
}
if user == nil {
return fmt.Errorf("user not found")
}
return nil
}
func (ctx *AuthBDDTestContext) iHaveAUserWithCredentialsInTheStore() error {
hashedPassword, err := ctx.service.HashPassword("userpassword123!")
if err != nil {
return fmt.Errorf("failed to hash password: %v", err)
}
user := &User{
ID: "auth-user-123",
Email: "authuser@example.com",
PasswordHash: hashedPassword,
}
err = ctx.service.userStore.CreateUser(context.Background(), user)
if err != nil {
return fmt.Errorf("failed to create user: %v", err)
}
ctx.user = user
ctx.password = "userpassword123!"
return nil
}
func (ctx *AuthBDDTestContext) iAuthenticateWithCorrectCredentials() error {
// Implement authentication using GetUserByEmail and VerifyPassword
user, err := ctx.service.userStore.GetUserByEmail(context.Background(), ctx.user.Email)
if err != nil {
ctx.authError = err
return nil
}
err = ctx.service.VerifyPassword(user.PasswordHash, ctx.password)
if err != nil {
ctx.authError = err
return nil
}
ctx.authResult = user
return nil
}
func (ctx *AuthBDDTestContext) theAuthenticationShouldSucceed() error {
if ctx.authError != nil {
return fmt.Errorf("authentication failed: %v", ctx.authError)
}
if ctx.authResult == nil {
return fmt.Errorf("no user returned from authentication")
}
return nil
}
func (ctx *AuthBDDTestContext) theUserShouldBeReturned() error {
if ctx.authResult == nil {
return fmt.Errorf("no user returned")
}
if ctx.authResult.ID != ctx.user.ID {
return fmt.Errorf("wrong user returned: expected %s, got %s", ctx.user.ID, ctx.authResult.ID)
}
return nil
}
func (ctx *AuthBDDTestContext) iAuthenticateWithIncorrectCredentials() error {
// Implement authentication using GetUserByEmail and VerifyPassword
user, err := ctx.service.userStore.GetUserByEmail(context.Background(), ctx.user.Email)
if err != nil {
ctx.authError = err
return nil
}
err = ctx.service.VerifyPassword(user.PasswordHash, "wrongpassword")
if err != nil {
ctx.authError = err
return nil
}
ctx.authResult = user
return nil
}
func (ctx *AuthBDDTestContext) theAuthenticationShouldFail() error {
if ctx.authError == nil {
return fmt.Errorf("authentication should have failed")
}
return nil
}
func (ctx *AuthBDDTestContext) anErrorShouldBeReturned() error {
if ctx.authError == nil {
return fmt.Errorf("no error returned")
}
return nil
}
// User store-specific step registration
func (ctx *AuthBDDTestContext) registerUserStoreSteps(s *godog.ScenarioContext) {
// User store steps
s.Step(`^I have a user store configured$`, ctx.iHaveAUserStoreConfigured)
s.Step(`^I create a new user$`, ctx.iCreateANewUser)
s.Step(`^the user should be stored successfully$`, ctx.theUserShouldBeStoredSuccessfully)
s.Step(`^I should be able to retrieve the user by ID$`, ctx.iShouldBeAbleToRetrieveTheUserByID)
// Authentication steps
s.Step(`^I have a user with credentials in the store$`, ctx.iHaveAUserWithCredentialsInTheStore)
s.Step(`^I authenticate with correct credentials$`, ctx.iAuthenticateWithCorrectCredentials)
s.Step(`^the authentication should succeed$`, ctx.theAuthenticationShouldSucceed)
s.Step(`^the user should be returned$`, ctx.theUserShouldBeReturned)
s.Step(`^I authenticate with incorrect credentials$`, ctx.iAuthenticateWithIncorrectCredentials)
s.Step(`^the authentication should fail$`, ctx.theAuthenticationShouldFail)
s.Step(`^an error should be returned$`, ctx.anErrorShouldBeReturned)
}