11package scim
22
33import (
4+ "embed"
5+ "encoding/json"
46 "net/http"
57 "net/http/httptest"
8+ "net/url"
69 "testing"
710
811 "github.com/stretchr/testify/require"
9- "github.com/supabase/auth/internal/api/scim/fixtures"
1012 "github.com/supabase/auth/internal/api/scim/protocol"
1113 "github.com/supabase/auth/internal/conf"
1214)
1315
16+ //go:embed testdata/*
17+ var fixtures embed.FS
18+
1419func newServerFor (externalURL string ) * Server {
1520 return NewServer (& conf.GlobalConfiguration {
1621 API : conf.APIConfiguration {ExternalURL : externalURL },
1722 })
1823}
1924
25+ func testFixture (t * testing.T , file string ) string {
26+ data , err := fixtures .ReadFile ("testdata/" + file )
27+ require .NoError (t , err )
28+ return string (data )
29+ }
30+
2031func TestServer (t * testing.T ) {
2132 srv := newServerFor ("http://localhost:9999" )
2233 require .NotNil (t , srv )
@@ -35,7 +46,7 @@ func TestServer(t *testing.T) {
3546
3647 require .Equal (t , http .StatusOK , w .Code )
3748 require .Equal (t , protocol .MediaType , w .Header ().Get ("Content-Type" ))
38- require .JSONEq (t , fixtures . ServiceProviderConfig , w .Body .String ())
49+ require .JSONEq (t , testFixture ( t , "service_provider_config.json" ) , w .Body .String ())
3950 })
4051
4152 for _ , tc := range []struct {
@@ -49,10 +60,23 @@ func TestServer(t *testing.T) {
4960 r := httptest .NewRequest (http .MethodGet , BasePath + "/" + tc .path , nil )
5061 w := httptest .NewRecorder ()
5162
63+ require .NoError (t , tc .handler (w , r ))
64+
65+ require .Equal (t , http .StatusOK , w .Code )
66+ require .Equal (t , protocol .MediaType , w .Header ().Get ("Content-Type" ))
67+ require .JSONEq (t , testFixture (t , "empty_list_response.json" ), w .Body .String ())
68+ })
69+
70+ t .Run (tc .path + " rejects filter query parameter" , func (t * testing.T ) {
71+ filter := url.Values {"filter" : {`name eq "User"` }}.Encode ()
72+ r := httptest .NewRequest (http .MethodGet , BasePath + "/" + tc .path + "?" + filter , nil )
73+ w := httptest .NewRecorder ()
74+
5275 var scimErr * protocol.Error
5376 require .ErrorAs (t , tc .handler (w , r ), & scimErr )
5477
55- require .Equal (t , http .StatusNotImplemented , scimErr .StatusCode ())
78+ require .Equal (t , http .StatusForbidden , scimErr .StatusCode ())
79+ requireMarshalsTo (t , testFixture (t , "filter_forbidden.json" ), scimErr )
5680 })
5781 }
5882
@@ -64,7 +88,7 @@ func TestServer(t *testing.T) {
6488 require .ErrorAs (t , srv .NotFound (w , r ), & scimErr )
6589
6690 require .Equal (t , http .StatusNotFound , scimErr .StatusCode ())
67- require . Equal (t , "Endpoint or resource does not exist" , scimErr . Detail )
91+ requireMarshalsTo (t , testFixture ( t , "not_found.json" ) , scimErr )
6892 })
6993
7094 t .Run ("NotAllowed" , func (t * testing.T ) {
@@ -76,5 +100,14 @@ func TestServer(t *testing.T) {
76100
77101 require .Equal (t , http .StatusMethodNotAllowed , scimErr .StatusCode ())
78102 require .Equal (t , http .MethodGet , w .Header ().Get ("Allow" ))
103+ requireMarshalsTo (t , testFixture (t , "method_not_allowed.json" ), scimErr )
79104 })
80105}
106+
107+ func requireMarshalsTo (t * testing.T , expected string , v any ) {
108+ t .Helper ()
109+
110+ body , err := json .Marshal (v )
111+ require .NoError (t , err )
112+ require .JSONEq (t , expected , string (body ))
113+ }
0 commit comments