github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/modules/oauth/oauth_test.go (about) 1 package oauth_test 2 3 import ( 4 "context" 5 "fmt" 6 "io/ioutil" 7 "net/http" 8 "os" 9 "testing" 10 11 "github.com/covergates/covergates/config" 12 "github.com/covergates/covergates/core" 13 "github.com/covergates/covergates/models" 14 "github.com/covergates/covergates/modules/oauth" 15 "github.com/drone/go-scm/scm" 16 "github.com/google/go-cmp/cmp" 17 log "github.com/sirupsen/logrus" 18 "gorm.io/driver/sqlite" 19 "gorm.io/gorm" 20 ) 21 22 var service *oauth.Service 23 var userStore core.UserStore 24 var conf *config.Config 25 26 func mockUsers(store core.UserStore) { 27 store.Create(core.Gitea, &scm.User{Login: "user1"}, &core.Token{}) 28 store.Create(core.Gitea, &scm.User{Login: "user2"}, &core.Token{}) 29 } 30 31 func TestMain(m *testing.M) { 32 log.SetReportCaller(true) 33 cwd, _ := os.Getwd() 34 tempFile, err := ioutil.TempFile(cwd, "*.db") 35 if err != nil { 36 log.Fatal(err) 37 } 38 tempFile.Close() 39 x, err := gorm.Open(sqlite.Open(tempFile.Name()), &gorm.Config{}) 40 if err != nil { 41 log.Fatal(err) 42 } 43 dbService := models.NewDatabaseService(x) 44 userStore = &models.UserStore{DB: dbService} 45 oauthStore := &models.OAuthStore{DB: dbService} 46 dbService.Migrate() 47 mockUsers(userStore) 48 49 conf = &config.Config{} 50 service = oauth.NewService(conf, oauthStore, userStore) 51 exit := m.Run() 52 os.Remove(tempFile.Name()) 53 os.Exit(exit) 54 } 55 56 func TestCreate(t *testing.T) { 57 ctx := context.Background() 58 if _, err := service.CreateToken(ctx, ""); err == nil || err != oauth.ErrTokenOwnerNotFound { 59 t.Fatal("should check token owner in contex") 60 } 61 62 user, err := userStore.FindByLogin("user1") 63 if err != nil { 64 t.Fatal(err) 65 } 66 67 ctx = service.WithUser(ctx, user) 68 69 token, err := service.CreateToken(ctx, "test_token") 70 if err != nil { 71 t.Fatal(err) 72 } 73 74 if token.Access == "" || token.Name != "test_token" { 75 t.Fatal() 76 } 77 78 if diff := cmp.Diff(user, token.Owner); diff != "" { 79 t.Fatal(diff) 80 } 81 } 82 83 func TestValidate(t *testing.T) { 84 85 user, err := userStore.FindByLogin("user1") 86 if err != nil { 87 t.Fatal(err) 88 } 89 90 ctx := service.WithUser(context.Background(), user) 91 92 token, err := service.CreateToken(ctx, "validate_token") 93 94 if err != nil { 95 t.Fatal(err) 96 } 97 98 request, _ := http.NewRequest( 99 "GET", 100 fmt.Sprintf("%s?access_token=%s", conf.Server.Addr, token.Access), 101 nil, 102 ) 103 104 tokenOwner, err := service.Validate(request) 105 if err != nil { 106 t.Fatal(err) 107 } 108 if diff := cmp.Diff(user, tokenOwner); diff != "" { 109 t.Fatal(diff) 110 } 111 112 request, _ = http.NewRequest( 113 "GET", 114 fmt.Sprintf("%s?access_token=123", conf.Server.Addr), 115 nil, 116 ) 117 if _, err := service.Validate(request); err == nil { 118 t.Fatal("should return err for invalid token") 119 } 120 } 121 122 func TestDelete(t *testing.T) { 123 user, err := userStore.FindByLogin("user1") 124 if err != nil { 125 t.Fatal(err) 126 } 127 128 ctx := service.WithUser(context.Background(), user) 129 130 token, err := service.CreateToken(ctx, "delete_token") 131 132 if err != nil { 133 t.Fatal(err) 134 } 135 136 if err := service.DeleteToken(ctx, token); err != nil { 137 t.Fatal(err) 138 } 139 140 request, _ := http.NewRequest( 141 "GET", 142 fmt.Sprintf("%s?access_token=%s", conf.Server.Addr, token.Access), 143 nil, 144 ) 145 if _, err := service.Validate(request); err == nil { 146 t.Fatal("should return err for deleted token") 147 } 148 149 token, err = service.CreateToken(ctx, "user1_token") 150 if err != nil { 151 t.Fatal(err) 152 } 153 154 user2, err := userStore.FindByLogin("user2") 155 if err != nil { 156 t.Fatal(err) 157 } 158 ctx = service.WithUser(context.Background(), user2) 159 if err := service.DeleteToken(ctx, token); err == nil { 160 t.Fatal("user2 cannot delete user1's token") 161 } 162 } 163 164 func TestList(t *testing.T) { 165 user, err := userStore.FindByLogin("user2") 166 if err != nil { 167 t.Fatal(err) 168 } 169 170 names := []string{"token1", "token2"} 171 172 ctx := service.WithUser(context.Background(), user) 173 for _, name := range names { 174 service.CreateToken(ctx, name) 175 } 176 177 tokens, err := service.ListTokens(ctx) 178 if err != nil { 179 t.Fatal(err) 180 } 181 182 tokenNames := make([]string, len(tokens)) 183 for i, token := range tokens { 184 tokenNames[i] = token.Name 185 } 186 if diff := cmp.Diff(names, tokenNames); diff != "" { 187 t.Fatal(diff) 188 } 189 }