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.

73 lines
2.2 KiB

  1. // Copyright 2020 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. "github.com/tstranex/u2f"
  9. )
  10. func TestGetU2FRegistrationByID(t *testing.T) {
  11. assert.NoError(t, PrepareTestDatabase())
  12. res, err := GetU2FRegistrationByID(1)
  13. assert.NoError(t, err)
  14. assert.Equal(t, "U2F Key", res.Name)
  15. _, err = GetU2FRegistrationByID(342432)
  16. assert.Error(t, err)
  17. assert.True(t, IsErrU2FRegistrationNotExist(err))
  18. }
  19. func TestGetU2FRegistrationsByUID(t *testing.T) {
  20. assert.NoError(t, PrepareTestDatabase())
  21. res, err := GetU2FRegistrationsByUID(1)
  22. assert.NoError(t, err)
  23. assert.Len(t, res, 1)
  24. assert.Equal(t, "U2F Key", res[0].Name)
  25. }
  26. func TestU2FRegistration_TableName(t *testing.T) {
  27. assert.Equal(t, "u2f_registration", U2FRegistration{}.TableName())
  28. }
  29. func TestU2FRegistration_UpdateCounter(t *testing.T) {
  30. assert.NoError(t, PrepareTestDatabase())
  31. reg := AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration)
  32. reg.Counter = 1
  33. assert.NoError(t, reg.UpdateCounter())
  34. AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 1})
  35. }
  36. func TestU2FRegistration_UpdateLargeCounter(t *testing.T) {
  37. assert.NoError(t, PrepareTestDatabase())
  38. reg := AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration)
  39. reg.Counter = 0xffffffff
  40. assert.NoError(t, reg.UpdateCounter())
  41. AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 0xffffffff})
  42. }
  43. func TestCreateRegistration(t *testing.T) {
  44. assert.NoError(t, PrepareTestDatabase())
  45. user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  46. res, err := CreateRegistration(user, "U2F Created Key", &u2f.Registration{Raw: []byte("Test")})
  47. assert.NoError(t, err)
  48. assert.Equal(t, "U2F Created Key", res.Name)
  49. assert.Equal(t, []byte("Test"), res.Raw)
  50. AssertExistsIf(t, true, &U2FRegistration{Name: "U2F Created Key", UserID: user.ID})
  51. }
  52. func TestDeleteRegistration(t *testing.T) {
  53. assert.NoError(t, PrepareTestDatabase())
  54. reg := AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration)
  55. assert.NoError(t, DeleteRegistration(reg))
  56. AssertNotExistsBean(t, &U2FRegistration{ID: 1})
  57. }