|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LightSaml\SpBundle\Tests\Security\User; |
| 4 | + |
| 5 | +use LightSaml\Model\Assertion\Assertion; |
| 6 | +use LightSaml\Model\Assertion\Attribute; |
| 7 | +use LightSaml\Model\Assertion\AttributeStatement; |
| 8 | +use LightSaml\Model\Protocol\Response; |
| 9 | +use LightSaml\SpBundle\Security\Authentication\Token\SamlSpResponseToken; |
| 10 | +use LightSaml\SpBundle\Security\User\SimpleAttributeMapper; |
| 11 | + |
| 12 | +class SimpleAttributeMapperTest extends \PHPUnit_Framework_TestCase |
| 13 | +{ |
| 14 | + public function test_get_attributes_from_single_assertion_response() |
| 15 | + { |
| 16 | + $assertion = $this->buildAssertion([ |
| 17 | + 'organization' => 'test', |
| 18 | + 'name' => 'John', |
| 19 | + 'email_address' => 'john@domain.com', |
| 20 | + 'test' => ['one', 'two'], |
| 21 | + ]); |
| 22 | + $response = $this->buildResponse($assertion); |
| 23 | + $samlSpResponseToken = $this->buildSamlSpResponseToken($response); |
| 24 | + |
| 25 | + $expectedAttributes = [ |
| 26 | + 'organization' => 'test', |
| 27 | + 'name' => 'John', |
| 28 | + 'email_address' => 'john@domain.com', |
| 29 | + 'test' => ['one', 'two'], |
| 30 | + ]; |
| 31 | + |
| 32 | + $simpleAttributeMapper = new SimpleAttributeMapper(); |
| 33 | + $actualAttributes = $simpleAttributeMapper->getAttributes($samlSpResponseToken); |
| 34 | + |
| 35 | + $this->assertEquals($expectedAttributes, $actualAttributes); |
| 36 | + } |
| 37 | + |
| 38 | + public function test_get_attributes_from_multi_assertions_response() |
| 39 | + { |
| 40 | + $assertion = $this->buildAssertion([ |
| 41 | + 'organization' => 'test', |
| 42 | + 'name' => 'John', |
| 43 | + 'email_address' => 'john@domain.com', |
| 44 | + 'test' => ['one', 'two'], |
| 45 | + ]); |
| 46 | + $response = $this->buildResponse($assertion); |
| 47 | + |
| 48 | + $assertion = $this->buildAssertion([ |
| 49 | + 'name' => 'Doe', |
| 50 | + 'email_address' => 'doe@domain.com', |
| 51 | + 'test' => ['three', 'four'], |
| 52 | + ]); |
| 53 | + $response = $this->buildResponse($assertion, $response); |
| 54 | + |
| 55 | + $samlSpResponseToken = $this->buildSamlSpResponseToken($response); |
| 56 | + |
| 57 | + $expectedAttributes = [ |
| 58 | + 'organization' => 'test', |
| 59 | + 'name' => ['John', 'Doe'], |
| 60 | + 'email_address' => ['john@domain.com', 'doe@domain.com'], |
| 61 | + 'test' => ['one', 'two', 'three', 'four'], |
| 62 | + ]; |
| 63 | + |
| 64 | + $simpleAttributeMapper = new SimpleAttributeMapper(); |
| 65 | + $actualAttributes = $simpleAttributeMapper->getAttributes($samlSpResponseToken); |
| 66 | + |
| 67 | + $this->assertEquals($expectedAttributes, $actualAttributes); |
| 68 | + } |
| 69 | + |
| 70 | + public function test_get_attributes_from_multi_attribute_statements_response() |
| 71 | + { |
| 72 | + $assertion = $this->buildAssertion([ |
| 73 | + 'organization' => 'test', |
| 74 | + 'name' => 'John', |
| 75 | + 'email_address' => 'john@domain.com', |
| 76 | + 'test' => ['one', 'two'] |
| 77 | + ]); |
| 78 | + $assertion = $this->buildAssertion([ |
| 79 | + 'name' => 'Doe', |
| 80 | + 'email_address' => 'doe@domain.com', |
| 81 | + 'test' => ['three', 'four'] |
| 82 | + ], $assertion); |
| 83 | + $response = $this->buildResponse($assertion); |
| 84 | + |
| 85 | + $samlSpResponseToken = $this->buildSamlSpResponseToken($response); |
| 86 | + |
| 87 | + $expectedAttributes = [ |
| 88 | + 'organization' => 'test', |
| 89 | + 'name' => ['John', 'Doe'], |
| 90 | + 'email_address' => ['john@domain.com', 'doe@domain.com'], |
| 91 | + 'test' => ['one', 'two', 'three', 'four'], |
| 92 | + ]; |
| 93 | + |
| 94 | + $simpleAttributeMapper = new SimpleAttributeMapper(); |
| 95 | + $actualAttributes = $simpleAttributeMapper->getAttributes($samlSpResponseToken); |
| 96 | + |
| 97 | + $this->assertEquals($expectedAttributes, $actualAttributes); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @param Response $response |
| 102 | + * |
| 103 | + * @return \LightSaml\SpBundle\Security\Authentication\Token\SamlSpResponseToken |
| 104 | + */ |
| 105 | + private function buildSamlSpResponseToken(Response $response) |
| 106 | + { |
| 107 | + return new SamlSpResponseToken($response, 'test'); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param Assertion $assertion |
| 112 | + * @param Response $response |
| 113 | + * |
| 114 | + * @return Response |
| 115 | + */ |
| 116 | + private function buildResponse(Assertion $assertion, Response $response = null) |
| 117 | + { |
| 118 | + if (null == $response) { |
| 119 | + $response = new Response(); |
| 120 | + } |
| 121 | + |
| 122 | + $response->addAssertion($assertion); |
| 123 | + |
| 124 | + return $response; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @param array $assertionAttributes |
| 129 | + * @param Assertion $assertion |
| 130 | + * |
| 131 | + * @return Assertion |
| 132 | + */ |
| 133 | + private function buildAssertion(array $assertionAttributes, Assertion $assertion = null) |
| 134 | + { |
| 135 | + if (null == $assertion) { |
| 136 | + $assertion = new Assertion(); |
| 137 | + } |
| 138 | + |
| 139 | + $assertion->addItem($attributeStatement = new AttributeStatement()); |
| 140 | + foreach ($assertionAttributes as $attributeName => $attributeValue) { |
| 141 | + $attributeStatement->addAttribute(new Attribute($attributeName, $attributeValue)); |
| 142 | + } |
| 143 | + |
| 144 | + return $assertion; |
| 145 | + } |
| 146 | +} |
0 commit comments