cas_test.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cas
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func TestFetchAttributes(t *testing.T) {
  20. cases := []struct {
  21. Xml string
  22. Want map[string][]string
  23. }{
  24. {
  25. Xml: `<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
  26. <cas:authenticationSuccess>
  27. <cas:user>casuser</cas:user>
  28. <cas:proj>casproj</cas:proj>
  29. </cas:authenticationSuccess>
  30. </cas:serviceResponse>`,
  31. Want: map[string][]string{
  32. "cas:user": {"casuser"},
  33. "cas:proj": {"casproj"},
  34. },
  35. },
  36. {
  37. Xml: `<?xml version="1.0" encoding="UTF-8"?>
  38. <cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas"><cas:authenticationSuccess><cas:user>lcftest0416</cas:user><cas:proj>周凌测试无线公司1112342</cas:proj></cas:authenticationSuccess></cas:serviceResponse>`,
  39. Want: map[string][]string{
  40. "cas:user": {"lcftest0416"},
  41. "cas:proj": {"周凌测试无线公司1112342"},
  42. },
  43. },
  44. {
  45. Xml: `<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
  46. <cas:authenticationSuccess>
  47. <cas:user>casuser</cas:user>
  48. <cas:attributes>
  49. <cas:credentialType>UsernamePasswordCredential</cas:credentialType>
  50. <cas:isFromNewLogin>false</cas:isFromNewLogin>
  51. <cas:authenticationDate>2019-09-05T12:40:08.014Z[UTC]</cas:authenticationDate>
  52. <cas:authenticationMethod>AcceptUsersAuthenticationHandler</cas:authenticationMethod>
  53. <cas:successfulAuthenticationHandlers>AcceptUsersAuthenticationHandler</cas:successfulAuthenticationHandlers>
  54. <cas:longTermAuthenticationRequestTokenUsed>false</cas:longTermAuthenticationRequestTokenUsed>
  55. </cas:attributes>
  56. </cas:authenticationSuccess>
  57. </cas:serviceResponse>`,
  58. Want: map[string][]string{
  59. "cas:user": {"casuser"},
  60. "cas:credentialType": {"UsernamePasswordCredential"},
  61. "cas:isFromNewLogin": {"false"},
  62. "cas:authenticationDate": {"2019-09-05T12:40:08.014Z[UTC]"},
  63. "cas:authenticationMethod": {"AcceptUsersAuthenticationHandler"},
  64. "cas:successfulAuthenticationHandlers": {"AcceptUsersAuthenticationHandler"},
  65. "cas:longTermAuthenticationRequestTokenUsed": {"false"},
  66. },
  67. },
  68. {
  69. Xml: `<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
  70. <cas:authenticationSuccess>
  71. <cas:user>casuser</cas:user>
  72. </cas:authenticationSuccess>
  73. </cas:serviceResponse>`,
  74. Want: map[string][]string{
  75. "cas:user": {"casuser"},
  76. },
  77. },
  78. }
  79. for _, c := range cases {
  80. got := fetchAttributes([]byte(c.Xml))
  81. if !reflect.DeepEqual(got, c.Want) {
  82. t.Errorf("want %s got %s", c.Want, got)
  83. }
  84. }
  85. }