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"
14+
15+ _ "embed"
1216)
1317
18+ //go:embed testdata/*
19+ var fixtures embed.FS
20+
1421func newServerFor (externalURL string ) * Server {
1522 return NewServer (& conf.GlobalConfiguration {
1623 API : conf.APIConfiguration {ExternalURL : externalURL },
1724 })
1825}
1926
27+ func testFixture (file string ) string {
28+ data , _ := fixtures .ReadFile ("testdata/" + file )
29+ return string (data )
30+ }
31+
2032func TestServer (t * testing.T ) {
2133 srv := newServerFor ("http://localhost:9999" )
2234 require .NotNil (t , srv )
@@ -35,7 +47,7 @@ func TestServer(t *testing.T) {
3547
3648 require .Equal (t , http .StatusOK , w .Code )
3749 require .Equal (t , protocol .MediaType , w .Header ().Get ("Content-Type" ))
38- require .JSONEq (t , fixtures . ServiceProviderConfig , w .Body .String ())
50+ require .JSONEq (t , testFixture ( "service_provider_config.json" ) , w .Body .String ())
3951 })
4052
4153 for _ , tc := range []struct {
@@ -49,10 +61,23 @@ func TestServer(t *testing.T) {
4961 r := httptest .NewRequest (http .MethodGet , BasePath + "/" + tc .path , nil )
5062 w := httptest .NewRecorder ()
5163
64+ require .NoError (t , tc .handler (w , r ))
65+
66+ require .Equal (t , http .StatusOK , w .Code )
67+ require .Equal (t , protocol .MediaType , w .Header ().Get ("Content-Type" ))
68+ require .JSONEq (t , testFixture ("empty_list_response.json" ), w .Body .String ())
69+ })
70+
71+ t .Run (tc .path + " rejects filter query parameter" , func (t * testing.T ) {
72+ filter := url.Values {"filter" : {`name eq "User"` }}.Encode ()
73+ r := httptest .NewRequest (http .MethodGet , BasePath + "/" + tc .path + "?" + filter , nil )
74+ w := httptest .NewRecorder ()
75+
5276 var scimErr * protocol.Error
5377 require .ErrorAs (t , tc .handler (w , r ), & scimErr )
5478
55- require .Equal (t , http .StatusNotImplemented , scimErr .StatusCode ())
79+ require .Equal (t , http .StatusForbidden , scimErr .StatusCode ())
80+ requireMarshalsTo (t , testFixture ("filter_forbidden.json" ), scimErr )
5681 })
5782 }
5883
@@ -64,7 +89,7 @@ func TestServer(t *testing.T) {
6489 require .ErrorAs (t , srv .NotFound (w , r ), & scimErr )
6590
6691 require .Equal (t , http .StatusNotFound , scimErr .StatusCode ())
67- require . Equal (t , "Endpoint or resource does not exist" , scimErr . Detail )
92+ requireMarshalsTo (t , testFixture ( "not_found.json" ) , scimErr )
6893 })
6994
7095 t .Run ("NotAllowed" , func (t * testing.T ) {
@@ -76,5 +101,14 @@ func TestServer(t *testing.T) {
76101
77102 require .Equal (t , http .StatusMethodNotAllowed , scimErr .StatusCode ())
78103 require .Equal (t , http .MethodGet , w .Header ().Get ("Allow" ))
104+ requireMarshalsTo (t , testFixture ("method_not_allowed.json" ), scimErr )
79105 })
80106}
107+
108+ func requireMarshalsTo (t * testing.T , expected string , v any ) {
109+ t .Helper ()
110+
111+ body , err := json .Marshal (v )
112+ require .NoError (t , err )
113+ require .JSONEq (t , expected , string (body ))
114+ }
0 commit comments