-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from fabiofenoglio/idp-groups-compatibility
Implement compatibility with different IDP group formats without impacting the current API
- Loading branch information
Showing
2 changed files
with
211 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package gincloudflareaccess | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_mapIDPGroupsToCloudflareGroups(t *testing.T) { | ||
expectedErrBadFormat := "groups entry is not an array" | ||
expectedErrBadIDPFormat := "unknown format for IDP group entry" | ||
|
||
tests := []struct { | ||
input string | ||
want []CloudflareIdentityGroup | ||
wantErr string | ||
}{ | ||
{input: `null`, want: []CloudflareIdentityGroup{}}, | ||
{input: `[]`, want: []CloudflareIdentityGroup{}}, | ||
{input: `{}`, wantErr: expectedErrBadFormat}, | ||
{input: `123`, wantErr: expectedErrBadFormat}, | ||
{input: `"aaa"`, wantErr: expectedErrBadFormat}, | ||
{input: `[null]`, wantErr: expectedErrBadIDPFormat}, | ||
{input: `[123]`, wantErr: expectedErrBadIDPFormat}, | ||
{input: `[[]]`, wantErr: expectedErrBadIDPFormat}, | ||
{input: `[{}]`, wantErr: expectedErrBadIDPFormat}, | ||
{input: `[{"name": "just a name"}]`, wantErr: expectedErrBadIDPFormat}, | ||
{input: `[{"another_field": 42}]`, wantErr: expectedErrBadIDPFormat}, | ||
{ | ||
input: `["group 1"]`, | ||
want: []CloudflareIdentityGroup{ | ||
{Id: "group 1", Name: "group 1"}, | ||
}, | ||
}, | ||
{ | ||
input: `[{"id": "g1"}]`, | ||
want: []CloudflareIdentityGroup{ | ||
{Id: "g1", Name: "g1"}, | ||
}, | ||
}, | ||
{ | ||
input: `[{"email": "[email protected]"}]`, | ||
want: []CloudflareIdentityGroup{ | ||
{Id: "[email protected]", Name: "[email protected]", Email: "[email protected]"}, | ||
}, | ||
}, | ||
{ | ||
input: `[ | ||
"G0", | ||
{"id": "G1"}, | ||
{"email": "[email protected]"}, | ||
"G3", | ||
{"id": "G4", "name": "group 4"}, | ||
{"id": "G5", "name": "group 5", "email": "[email protected]"}, | ||
{"id": "G6", "email": "[email protected]"} | ||
]`, | ||
want: []CloudflareIdentityGroup{ | ||
{Id: "G0", Name: "G0"}, | ||
{Id: "G1", Name: "G1"}, | ||
{Id: "[email protected]", Name: "[email protected]", Email: "[email protected]"}, | ||
{Id: "G3", Name: "G3"}, | ||
{Id: "G4", Name: "group 4"}, | ||
{Id: "G5", Name: "group 5", Email: "[email protected]"}, | ||
{Id: "G6", Name: "[email protected]", Email: "[email protected]"}, | ||
}, | ||
}, | ||
} | ||
for i, tt := range tests { | ||
t.Run(fmt.Sprintf("test_case_%d", i), func(t *testing.T) { | ||
var unmarshalled interface{} | ||
|
||
err := json.Unmarshal([]byte(tt.input), &unmarshalled) | ||
assert.NoError(t, err) | ||
|
||
got, err := mapIDPGroupsToCloudflareGroups(unmarshalled) | ||
|
||
if tt.wantErr != "" { | ||
assert.Error(t, err) | ||
assert.Contains(t, err.Error(), tt.wantErr) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
|
||
assert.Equalf(t, tt.want, got, "mapIDPGroupsToCloudflareGroups(%v)", tt.input) | ||
}) | ||
} | ||
} |