fediverse_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package fediverse
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/owncast/owncast/utils"
  6. )
  7. const (
  8. accessToken = "fake-access-token"
  9. account = "blah"
  10. userID = "fake-user-id"
  11. userDisplayName = "fake-user-display-name"
  12. )
  13. func TestOTPFlowValidation(t *testing.T) {
  14. r, success, err := RegisterFediverseOTP(accessToken, userID, userDisplayName, account)
  15. if err != nil {
  16. t.Error(err)
  17. }
  18. if !success {
  19. t.Error("Registration should be permitted.")
  20. }
  21. if r.Code == "" {
  22. t.Error("Code is empty")
  23. }
  24. if r.Account != account {
  25. t.Error("Account is not set correctly")
  26. }
  27. if r.Timestamp.IsZero() {
  28. t.Error("Timestamp is empty")
  29. }
  30. valid, registration := ValidateFediverseOTP(accessToken, r.Code)
  31. if !valid {
  32. t.Error("Code is not valid")
  33. }
  34. if registration.Account != account {
  35. t.Error("Account is not set correctly")
  36. }
  37. if registration.UserID != userID {
  38. t.Error("UserID is not set correctly")
  39. }
  40. if registration.UserDisplayName != userDisplayName {
  41. t.Error("UserDisplayName is not set correctly")
  42. }
  43. }
  44. func TestSingleOTPFlowRequest(t *testing.T) {
  45. r1, _, _ := RegisterFediverseOTP(accessToken, userID, userDisplayName, account)
  46. r2, s2, _ := RegisterFediverseOTP(accessToken, userID, userDisplayName, account)
  47. if r1.Code != r2.Code {
  48. t.Error("Only one registration should be permitted.")
  49. }
  50. if s2 {
  51. t.Error("Second registration should not be permitted.")
  52. }
  53. }
  54. func TestAccountCaseInsensitive(t *testing.T) {
  55. account := "Account"
  56. accessToken := "another-fake-access-token"
  57. r1, _, _ := RegisterFediverseOTP(accessToken, userID, userDisplayName, account)
  58. _, reg1 := ValidateFediverseOTP(accessToken, r1.Code)
  59. // Simulate second auth with account in different case
  60. r2, _, _ := RegisterFediverseOTP(accessToken, userID, userDisplayName, strings.ToUpper(account))
  61. _, reg2 := ValidateFediverseOTP(accessToken, r2.Code)
  62. if reg1.Account != reg2.Account {
  63. t.Errorf("Account names should be case-insensitive: %s %s", reg1.Account, reg2.Account)
  64. }
  65. }
  66. func TestLimitGlobalPendingRequests(t *testing.T) {
  67. for i := 0; i < maxPendingRequests-1; i++ {
  68. at, _ := utils.GenerateRandomString(10)
  69. uid, _ := utils.GenerateRandomString(10)
  70. account, _ := utils.GenerateRandomString(10)
  71. _, success, error := RegisterFediverseOTP(at, uid, "userDisplayName", account)
  72. if !success {
  73. t.Error("Registration should be permitted.", i, " of ", len(pendingAuthRequests))
  74. }
  75. if error != nil {
  76. t.Error(error)
  77. }
  78. }
  79. // This one should fail
  80. at, _ := utils.GenerateRandomString(10)
  81. uid, _ := utils.GenerateRandomString(10)
  82. account, _ := utils.GenerateRandomString(10)
  83. _, success, error := RegisterFediverseOTP(at, uid, "userDisplayName", account)
  84. if success {
  85. t.Error("Registration should not be permitted.")
  86. }
  87. if error == nil {
  88. t.Error("Error should be returned.")
  89. }
  90. }