diff options
author | Sandro Santilli <strk@kbt.io> | 2017-03-20 09:31:08 +0100 |
---|---|---|
committer | Kim "BKC" Carlbäcker <kim.carlbacker@gmail.com> | 2017-03-20 09:31:08 +0100 |
commit | 9182a35f18b6d5cd981486852028e670984145c3 (patch) | |
tree | cab69a27330df5a095de95017e8b40bf13598889 /models | |
parent | e1586898b2f0905b6f59383c01a3aed6b39de93a (diff) | |
download | gitea-9182a35f18b6d5cd981486852028e670984145c3.tar.gz gitea-9182a35f18b6d5cd981486852028e670984145c3.zip |
Show user OpenID URIs in their profile (#1314)
Diffstat (limited to 'models')
-rw-r--r-- | models/fixtures/user_open_id.yml | 17 | ||||
-rw-r--r-- | models/migrations/migrations.go | 2 | ||||
-rw-r--r-- | models/migrations/v25.go | 18 | ||||
-rw-r--r-- | models/user_openid.go | 8 | ||||
-rw-r--r-- | models/user_openid_test.go | 82 |
5 files changed, 127 insertions, 0 deletions
diff --git a/models/fixtures/user_open_id.yml b/models/fixtures/user_open_id.yml new file mode 100644 index 0000000000..d3a367b99d --- /dev/null +++ b/models/fixtures/user_open_id.yml @@ -0,0 +1,17 @@ +- + id: 1 + uid: 1 + uri: https://user1.domain1.tld/ + show: false + +- + id: 2 + uid: 1 + uri: http://user1.domain2.tld/ + show: true + +- + id: 3 + uid: 2 + uri: https://domain1.tld/user2/ + show: true diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index d06a4473b8..f651a9b787 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -98,6 +98,8 @@ var migrations = []Migration{ NewMigration("add user openid table", addUserOpenID), // v24 -> v25 NewMigration("change the key_id and primary_key_id type", changeGPGKeysColumns), + // v25 -> v26 + NewMigration("add show field in user openid table", addUserOpenIDShow), } // Migrate database to current version diff --git a/models/migrations/v25.go b/models/migrations/v25.go new file mode 100644 index 0000000000..a8d746590a --- /dev/null +++ b/models/migrations/v25.go @@ -0,0 +1,18 @@ +// Copyright 2017 Gitea. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package migrations + +import ( + "fmt" + + "github.com/go-xorm/xorm" +) + +func addUserOpenIDShow(x *xorm.Engine) error { + if err := x.Sync2(new(UserOpenID)); err != nil { + return fmt.Errorf("Sync2: %v", err) + } + return nil +} diff --git a/models/user_openid.go b/models/user_openid.go index a5c88e9009..18e847d89d 100644 --- a/models/user_openid.go +++ b/models/user_openid.go @@ -21,6 +21,7 @@ type UserOpenID struct { ID int64 `xorm:"pk autoincr"` UID int64 `xorm:"INDEX NOT NULL"` URI string `xorm:"UNIQUE NOT NULL"` + Show bool `xorm:"DEFAULT false"` } // GetUserOpenIDs returns all openid addresses that belongs to given user. @@ -28,6 +29,7 @@ func GetUserOpenIDs(uid int64) ([]*UserOpenID, error) { openids := make([]*UserOpenID, 0, 5) if err := x. Where("uid=?", uid). + Asc("id"). Find(&openids); err != nil { return nil, err } @@ -89,6 +91,12 @@ func DeleteUserOpenID(openid *UserOpenID) (err error) { return nil } +// ToggleUserOpenIDVisibility toggles visibility of an openid address of given user. +func ToggleUserOpenIDVisibility(id int64) (err error) { + _, err = x.Exec("update user_open_id set show = not show where id = ?", id) + return err +} + // GetUserByOpenID returns the user object by given OpenID if exists. func GetUserByOpenID(uri string) (*User, error) { if len(uri) == 0 { diff --git a/models/user_openid_test.go b/models/user_openid_test.go new file mode 100644 index 0000000000..74e3cf6f00 --- /dev/null +++ b/models/user_openid_test.go @@ -0,0 +1,82 @@ +// Copyright 2017 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 TestGetUserOpenIDs(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + oids, err := GetUserOpenIDs(int64(1)) + if assert.NoError(t, err) { + assert.Len(t, oids, 2) + assert.Equal(t, oids[0].URI, "https://user1.domain1.tld/") + assert.False(t, oids[0].Show) + assert.Equal(t, oids[1].URI, "http://user1.domain2.tld/") + assert.True(t, oids[1].Show) + } + + oids, err = GetUserOpenIDs(int64(2)) + if assert.NoError(t, err) { + assert.Len(t, oids, 1) + assert.Equal(t, oids[0].URI, "https://domain1.tld/user2/") + assert.True(t, oids[0].Show) + } +} + +func TestGetUserByOpenID(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + + user, err := GetUserByOpenID("https://unknown") + if assert.Error(t, err) { + assert.True(t, IsErrUserNotExist(err)) + } + + user, err = GetUserByOpenID("https://user1.domain1.tld") + if assert.NoError(t, err) { + assert.Equal(t, user.ID, int64(1)) + } + + user, err = GetUserByOpenID("https://domain1.tld/user2/") + if assert.NoError(t, err) { + assert.Equal(t, user.ID, int64(2)) + } +} + +func TestToggleUserOpenIDVisibility(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + oids, err := GetUserOpenIDs(int64(2)) + if ! assert.NoError(t, err) { + return + } + assert.Len(t, oids, 1) + assert.True(t, oids[0].Show) + + err = ToggleUserOpenIDVisibility(oids[0].ID) + if ! assert.NoError(t, err) { + return + } + + oids, err = GetUserOpenIDs(int64(2)) + if assert.NoError(t, err) { + assert.Len(t, oids, 1) + assert.False(t, oids[0].Show) + } + err = ToggleUserOpenIDVisibility(oids[0].ID) + if ! assert.NoError(t, err) { + return + } + + oids, err = GetUserOpenIDs(int64(2)) + if ! assert.NoError(t, err) { + return + } + assert.Len(t, oids, 1) + assert.True(t, oids[0].Show) +} |