@@ -7,28 +7,58 @@ import (
77 "testing"
88
99 "github.com/afteracademy/goserve-example-api-server-postgres/api/auth/model"
10+ roleModel "github.com/afteracademy/goserve-example-api-server-postgres/api/user/model"
1011 "github.com/afteracademy/goserve-example-api-server-postgres/startup"
1112 "github.com/afteracademy/goserve/v2/network"
13+ "github.com/afteracademy/goserve/v2/utility"
1214 "github.com/stretchr/testify/assert"
1315)
1416
1517func TestIntegrationAuthController_SignupSuccess (t * testing.T ) {
1618 router , module , shutdown := startup .TestServer ()
19+ var role * roleModel.Role
20+ var apikey * model.ApiKey
1721 defer shutdown ()
1822
19- apikey , err := module .GetInstance ().AuthService .CreateApiKey ("test_key" , 1 , []model.Permission {"test" }, []string {"comment" })
23+ t .Cleanup (func () {
24+ if apikey != nil {
25+ module .GetInstance ().AuthService .DeleteApiKey (apikey )
26+ }
27+ })
28+
29+ t .Cleanup (func () {
30+ if role != nil {
31+ module .GetInstance ().UserService .DeleteRole (role )
32+ }
33+ })
34+
35+ t .Cleanup (func () {
36+ module .GetInstance ().UserService .RemoveUserByEmail ("test@abc.com" )
37+ })
38+
39+ key , err := utility .GenerateRandomString (6 )
40+ if err != nil {
41+ t .Fatalf ("could not create key: %v" , err )
42+ }
43+
44+ apikey , err = module .GetInstance ().AuthService .CreateApiKey (key , 1 , []model.Permission {"test" }, []string {"comment" })
2045 if err != nil {
2146 t .Fatalf ("could not create apikey: %v" , err )
2247 }
2348
49+ role , err = module .GetInstance ().UserService .CreateRole (roleModel .RoleCodeLearner )
50+ if err != nil {
51+ t .Fatalf ("could not create role: %v" , err )
52+ }
53+
2454 body := `{"email":"test@abc.com","password":"123456","name":"test name"}`
2555
2656 req , err := http .NewRequest ("POST" , "/auth/signup/basic" , bytes .NewBuffer ([]byte (body )))
2757 if err != nil {
2858 t .Fatalf ("could not create request: %v" , err )
2959 }
3060 req .Header .Set ("Content-Type" , "application/json" )
31- req .Header .Set (network .ApiKeyHeader , apikey .Key )
61+ req .Header .Add (network .ApiKeyHeader , apikey .Key )
3262
3363 rr := httptest .NewRecorder ()
3464 router .GetEngine ().ServeHTTP (rr , req )
@@ -40,13 +70,4 @@ func TestIntegrationAuthController_SignupSuccess(t *testing.T) {
4070 assert .Contains (t , rr .Body .String (), `"roles"` )
4171 assert .Contains (t , rr .Body .String (), `"tokens"` )
4272
43- _ , err = module .GetInstance ().AuthService .DeleteApiKey (apikey )
44- if err != nil {
45- t .Fatalf ("could not delete apikey: %v" , err )
46- }
47-
48- _ , err = module .GetInstance ().UserService .RemoveUserByEmail ("test@abc.com" )
49- if err != nil {
50- t .Fatalf ("could not delete user: %v" , err )
51- }
5273}
0 commit comments