本站源代码
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.

70 lines
2.0KB

  1. package models
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/tstranex/u2f"
  6. )
  7. func TestGetU2FRegistrationByID(t *testing.T) {
  8. assert.NoError(t, PrepareTestDatabase())
  9. res, err := GetU2FRegistrationByID(1)
  10. assert.NoError(t, err)
  11. assert.Equal(t, "U2F Key", res.Name)
  12. _, err = GetU2FRegistrationByID(342432)
  13. assert.Error(t, err)
  14. assert.True(t, IsErrU2FRegistrationNotExist(err))
  15. }
  16. func TestGetU2FRegistrationsByUID(t *testing.T) {
  17. assert.NoError(t, PrepareTestDatabase())
  18. res, err := GetU2FRegistrationsByUID(1)
  19. assert.NoError(t, err)
  20. assert.Len(t, res, 1)
  21. assert.Equal(t, "U2F Key", res[0].Name)
  22. }
  23. func TestU2FRegistration_TableName(t *testing.T) {
  24. assert.Equal(t, "u2f_registration", U2FRegistration{}.TableName())
  25. }
  26. func TestU2FRegistration_UpdateCounter(t *testing.T) {
  27. assert.NoError(t, PrepareTestDatabase())
  28. reg := AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration)
  29. reg.Counter = 1
  30. assert.NoError(t, reg.UpdateCounter())
  31. AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 1})
  32. }
  33. func TestU2FRegistration_UpdateLargeCounter(t *testing.T) {
  34. assert.NoError(t, PrepareTestDatabase())
  35. reg := AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration)
  36. reg.Counter = 0xffffffff
  37. assert.NoError(t, reg.UpdateCounter())
  38. AssertExistsIf(t, true, &U2FRegistration{ID: 1, Counter: 0xffffffff})
  39. }
  40. func TestCreateRegistration(t *testing.T) {
  41. assert.NoError(t, PrepareTestDatabase())
  42. user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
  43. res, err := CreateRegistration(user, "U2F Created Key", &u2f.Registration{Raw: []byte("Test")})
  44. assert.NoError(t, err)
  45. assert.Equal(t, "U2F Created Key", res.Name)
  46. assert.Equal(t, []byte("Test"), res.Raw)
  47. AssertExistsIf(t, true, &U2FRegistration{Name: "U2F Created Key", UserID: user.ID})
  48. }
  49. func TestDeleteRegistration(t *testing.T) {
  50. assert.NoError(t, PrepareTestDatabase())
  51. reg := AssertExistsAndLoadBean(t, &U2FRegistration{ID: 1}).(*U2FRegistration)
  52. assert.NoError(t, DeleteRegistration(reg))
  53. AssertNotExistsBean(t, &U2FRegistration{ID: 1})
  54. }
上海开阖软件有限公司 沪ICP备12045867号-1