api_auth_test.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package api
  7. import (
  8. "testing"
  9. "github.com/syncthing/syncthing/lib/config"
  10. )
  11. var guiCfg config.GUIConfiguration
  12. func init() {
  13. guiCfg.User = "user"
  14. guiCfg.SetPassword("pass")
  15. }
  16. func TestStaticAuthOK(t *testing.T) {
  17. t.Parallel()
  18. ok := authStatic("user", "pass", guiCfg)
  19. if !ok {
  20. t.Fatalf("should pass auth")
  21. }
  22. }
  23. func TestSimpleAuthUsernameFail(t *testing.T) {
  24. t.Parallel()
  25. ok := authStatic("userWRONG", "pass", guiCfg)
  26. if ok {
  27. t.Fatalf("should fail auth")
  28. }
  29. }
  30. func TestStaticAuthPasswordFail(t *testing.T) {
  31. t.Parallel()
  32. ok := authStatic("user", "passWRONG", guiCfg)
  33. if ok {
  34. t.Fatalf("should fail auth")
  35. }
  36. }
  37. func TestAuthLDAPSendsCorrectBindDNWithTemplate(t *testing.T) {
  38. t.Parallel()
  39. templatedDn := ldapTemplateBindDN("cn=%s,dc=some,dc=example,dc=com", "username")
  40. expectedDn := "cn=username,dc=some,dc=example,dc=com"
  41. if expectedDn != templatedDn {
  42. t.Fatalf("ldapTemplateBindDN should be %s != %s", expectedDn, templatedDn)
  43. }
  44. }
  45. func TestAuthLDAPSendsCorrectBindDNWithNoTemplate(t *testing.T) {
  46. t.Parallel()
  47. templatedDn := ldapTemplateBindDN("cn=fixedusername,dc=some,dc=example,dc=com", "username")
  48. expectedDn := "cn=fixedusername,dc=some,dc=example,dc=com"
  49. if expectedDn != templatedDn {
  50. t.Fatalf("ldapTemplateBindDN should be %s != %s", expectedDn, templatedDn)
  51. }
  52. }