aboutsummaryrefslogtreecommitdiffstats
path: root/models/token_test.go
diff options
context:
space:
mode:
authorEthan Koenig <ethantkoenig@gmail.com>2016-12-28 20:03:40 -0500
committerKim "BKC" Carlbäcker <kim.carlbacker@gmail.com>2016-12-31 10:17:45 +0100
commitde8b73dd9241375d78a08bb01910d2c820b5e481 (patch)
treebfa78d8524398eee3416b1c0cb5e8aaf4bdd0daa /models/token_test.go
parentc0904f1942071ce870ab9d87bd6c7f49f614ee82 (diff)
downloadgitea-de8b73dd9241375d78a08bb01910d2c820b5e481.tar.gz
gitea-de8b73dd9241375d78a08bb01910d2c820b5e481.zip
Unit tests for token and update models
Diffstat (limited to 'models/token_test.go')
-rw-r--r--models/token_test.go107
1 files changed, 107 insertions, 0 deletions
diff --git a/models/token_test.go b/models/token_test.go
new file mode 100644
index 0000000000..15207ceb65
--- /dev/null
+++ b/models/token_test.go
@@ -0,0 +1,107 @@
+// Copyright 2016 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package models
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestNewAccessToken(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+ token := &AccessToken{
+ UID: 3,
+ Name: "Token C",
+ }
+ assert.NoError(t, NewAccessToken(token))
+ sess := x.NewSession()
+ defer sess.Close()
+ has, err := sess.Get(*token)
+ assert.NoError(t, err)
+ assert.True(t, has)
+ assert.Equal(t, int64(3), token.UID)
+ assert.Equal(t, "Token C", token.Name)
+
+ invalidToken := &AccessToken{
+ ID: token.ID, // duplicate
+ UID: 2,
+ Name: "Token F",
+ }
+ assert.Error(t, NewAccessToken(invalidToken))
+}
+
+func TestGetAccessTokenBySHA(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+ token, err := GetAccessTokenBySHA("hash1")
+ assert.NoError(t, err)
+ assert.Equal(t, int64(1), token.UID)
+ assert.Equal(t, "Token A", token.Name)
+ assert.Equal(t, "hash1", token.Sha1)
+
+ token, err = GetAccessTokenBySHA("notahash")
+ assert.Error(t, err)
+ assert.True(t, IsErrAccessTokenNotExist(err))
+
+ token, err = GetAccessTokenBySHA("")
+ assert.Error(t, err)
+ assert.True(t, IsErrAccessTokenEmpty(err))
+}
+
+func TestListAccessTokens(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+ tokens, err := ListAccessTokens(1)
+ assert.NoError(t, err)
+ assert.Len(t, tokens, 2)
+ assert.Equal(t, int64(1), tokens[0].UID)
+ assert.Equal(t, int64(1), tokens[1].UID)
+ assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token A")
+ assert.Contains(t, []string{tokens[0].Name, tokens[1].Name}, "Token B")
+
+ tokens, err = ListAccessTokens(2)
+ assert.NoError(t, err)
+ assert.Len(t, tokens, 1)
+ assert.Equal(t, int64(2), tokens[0].UID)
+ assert.Equal(t, "Token A", tokens[0].Name)
+
+ tokens, err = ListAccessTokens(100)
+ assert.NoError(t, err)
+ assert.Empty(t, tokens)
+}
+
+func TestUpdateAccessToken(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+ token, err := GetAccessTokenBySHA("hash2")
+ assert.NoError(t, err)
+ token.Name = "Token Z"
+
+ assert.NoError(t, UpdateAccessToken(token))
+
+ sess := x.NewSession()
+ defer sess.Close()
+ has, err := sess.Get(token)
+ assert.NoError(t, err)
+ assert.True(t, has)
+ assert.Equal(t, token.Name, "Token Z")
+}
+
+func TestDeleteAccessTokenByID(t *testing.T) {
+ assert.NoError(t, PrepareTestDatabase())
+
+ token, err := GetAccessTokenBySHA("hash2")
+ assert.NoError(t, err)
+ assert.Equal(t, int64(1), token.UID)
+
+ assert.NoError(t, DeleteAccessTokenByID(token.ID, 1))
+ sess := x.NewSession()
+ defer sess.Close()
+ has, err := sess.Get(token)
+ assert.NoError(t, err)
+ assert.False(t, has)
+
+ err = DeleteAccessTokenByID(100, 100)
+ assert.Error(t, err)
+ assert.True(t, IsErrAccessTokenNotExist(err))
+}