You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.9 KiB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestGetUserOpenIDs(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. oids, err := GetUserOpenIDs(int64(1))
  12. if assert.NoError(t, err) {
  13. assert.Len(t, oids, 2)
  14. assert.Equal(t, oids[0].URI, "https://user1.domain1.tld/")
  15. assert.False(t, oids[0].Show)
  16. assert.Equal(t, oids[1].URI, "http://user1.domain2.tld/")
  17. assert.True(t, oids[1].Show)
  18. }
  19. oids, err = GetUserOpenIDs(int64(2))
  20. if assert.NoError(t, err) {
  21. assert.Len(t, oids, 1)
  22. assert.Equal(t, oids[0].URI, "https://domain1.tld/user2/")
  23. assert.True(t, oids[0].Show)
  24. }
  25. }
  26. func TestGetUserByOpenID(t *testing.T) {
  27. assert.NoError(t, PrepareTestDatabase())
  28. user, err := GetUserByOpenID("https://unknown")
  29. if assert.Error(t, err) {
  30. assert.True(t, IsErrUserNotExist(err))
  31. }
  32. user, err = GetUserByOpenID("https://user1.domain1.tld")
  33. if assert.NoError(t, err) {
  34. assert.Equal(t, user.ID, int64(1))
  35. }
  36. user, err = GetUserByOpenID("https://domain1.tld/user2/")
  37. if assert.NoError(t, err) {
  38. assert.Equal(t, user.ID, int64(2))
  39. }
  40. }
  41. func TestToggleUserOpenIDVisibility(t *testing.T) {
  42. assert.NoError(t, PrepareTestDatabase())
  43. oids, err := GetUserOpenIDs(int64(2))
  44. if !assert.NoError(t, err) {
  45. return
  46. }
  47. assert.Len(t, oids, 1)
  48. assert.True(t, oids[0].Show)
  49. err = ToggleUserOpenIDVisibility(oids[0].ID)
  50. if !assert.NoError(t, err) {
  51. return
  52. }
  53. oids, err = GetUserOpenIDs(int64(2))
  54. if assert.NoError(t, err) {
  55. assert.Len(t, oids, 1)
  56. assert.False(t, oids[0].Show)
  57. }
  58. err = ToggleUserOpenIDVisibility(oids[0].ID)
  59. if !assert.NoError(t, err) {
  60. return
  61. }
  62. oids, err = GetUserOpenIDs(int64(2))
  63. if !assert.NoError(t, err) {
  64. return
  65. }
  66. assert.Len(t, oids, 1)
  67. assert.True(t, oids[0].Show)
  68. }