user_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package model
  5. import "testing"
  6. func TestValidateUserCreation(t *testing.T) {
  7. user := &User{}
  8. if err := user.ValidateUserCreation(); err == nil {
  9. t.Error(`An empty user should generate an error`)
  10. }
  11. user = &User{Username: "test", Password: ""}
  12. if err := user.ValidateUserCreation(); err == nil {
  13. t.Error(`User without password should generate an error`)
  14. }
  15. user = &User{Username: "test", Password: "a"}
  16. if err := user.ValidateUserCreation(); err == nil {
  17. t.Error(`Passwords shorter than 6 characters should generate an error`)
  18. }
  19. user = &User{Username: "", Password: "secret"}
  20. if err := user.ValidateUserCreation(); err == nil {
  21. t.Error(`An empty username should generate an error`)
  22. }
  23. user = &User{Username: "test", Password: "secret"}
  24. if err := user.ValidateUserCreation(); err != nil {
  25. t.Error(`A valid user should not generate any error`)
  26. }
  27. }
  28. func TestValidateUserModification(t *testing.T) {
  29. user := &User{}
  30. if err := user.ValidateUserModification(); err != nil {
  31. t.Error(`There is no changes, so we should not have an error`)
  32. }
  33. user = &User{Theme: "default"}
  34. if err := user.ValidateUserModification(); err != nil {
  35. t.Error(`A valid theme should not generate any errors`)
  36. }
  37. user = &User{Theme: "invalid theme"}
  38. if err := user.ValidateUserModification(); err == nil {
  39. t.Error(`An invalid theme should generate an error`)
  40. }
  41. user = &User{Password: "test123"}
  42. if err := user.ValidateUserModification(); err != nil {
  43. t.Error(`A valid password should not generate any errors`)
  44. }
  45. user = &User{Password: "a"}
  46. if err := user.ValidateUserModification(); err == nil {
  47. t.Error(`An invalid password should generate an error`)
  48. }
  49. }