-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.text
177 lines (141 loc) · 4.41 KB
/
random.text
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
db.createUser(
{
user: "ecommerce-api",
pwd: "ecommerceapp001",
roles: [ { role: "readWrite", db: "ecommerce" } ]
}
)
package controller
import (
"bytes"
"context"
"encoding/json"
"log"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/Emmrys-Jay/ecommerce-api/endpoints"
"github.com/Emmrys-Jay/ecommerce-api/entity"
"github.com/Emmrys-Jay/ecommerce-api/middleware"
"github.com/Emmrys-Jay/ecommerce-api/util"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
"golang.org/x/crypto/bcrypt"
)
var server = gin.Default()
func hashPassword(password string) string {
hp, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
log.Fatalln("Could not hash password")
}
return string(hp)
}
func initializeUserRoutes(database *mongo.Database) {
mdw := middleware.AuthorizeJWT()
endpoints.InitializeUserEndpoints(database, server, mdw)
}
func connectDB() *mongo.Database {
ctx := context.Background()
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017"))
if err != nil {
log.Fatalln("could not connect to server: ", err)
}
if err = client.Ping(ctx, readpref.Primary()); err != nil {
log.Fatalln("could not ping server: ", err)
}
return client.Database("ecommerce_test")
}
func configureDbCollections(db *mongo.Database) error {
collection := db.Collection("users")
options := options.Index()
options.SetUnique(true)
_, err := collection.Indexes().CreateOne(context.Background(), mongo.IndexModel{
Keys: bson.M{"username": 1},
Options: options,
})
return err
}
func deleteUsers(database *mongo.Database) error {
c := database.Collection("users")
_, err := c.DeleteMany(context.Background(), bson.M{})
return err
}
func TestCreateUser(t *testing.T) {
database := connectDB()
err := configureDbCollections(database)
require.NoError(t, err)
initializeUserRoutes(database)
godotenv.Load("../load.env")
username := util.RandomString()
user := entity.User{
Username: username,
Fullname: "Thompson" + username,
HashedPassword: hashPassword("101" + username),
Email: username + "@email.com",
}
userJson, _ := json.Marshal(user)
req, err := http.NewRequest("POST", "/user/create", bytes.NewBuffer(userJson))
if err != nil {
log.Fatalln("Could not create request")
}
recorder := httptest.NewRecorder()
server.ServeHTTP(recorder, req)
var expectedBody = struct {
ID string `json:"_id"`
Username string `json:"username"`
Fullname string `json:"fullname"`
Email string `json:"email"`
EmailIsVerfied bool `json:"email_is_verified"`
MobileNumber string `json:"mobile_number,omitempty"`
CreatedAt time.Time `json:"created_at"`
}{}
err = json.Unmarshal(recorder.Body.Bytes(), &expectedBody)
require.NoError(t, err)
require.Equal(t, "", expectedBody.MobileNumber)
require.Equal(t, http.StatusOK, recorder.Code)
require.NotZero(t, expectedBody.ID)
require.Equal(t, user.Username, expectedBody.Username)
require.Equal(t, user.Fullname, expectedBody.Fullname)
require.Equal(t, user.Email, expectedBody.Email)
require.Equal(t, false, expectedBody.EmailIsVerfied)
require.True(t, expectedBody.CreatedAt.Before(time.Now()))
err = deleteUsers(database)
require.NoError(t, err)
}
func TestUniqueUsernameField(t *testing.T) {
database := connectDB()
err := configureDbCollections(database)
require.NoError(t, err)
initializeUserRoutes(database)
gin.SetMode(gin.TestMode)
godotenv.Load("../load.env")
username := util.RandomString()
user := entity.User{
Username: username,
Fullname: "Thompson " + username,
HashedPassword: hashPassword("101" + username),
Email: username + "@email.com",
}
userJson, _ := json.Marshal(user)
req, err := http.NewRequest("POST", "/user/create", bytes.NewBuffer(userJson))
if err != nil {
log.Fatalln("Could not create request")
}
recorder := httptest.NewRecorder()
server.ServeHTTP(recorder, req)
require.Equal(t, 200, recorder.Code)
server.ServeHTTP(recorder, req)
require.Equal(t, 400, recorder.Code)
err = deleteUsers(database)
require.Error(t, err)
}
func TestLoginUser(t *testing.T) {
//database := connectDB()
godotenv.Load("../load.env")
}