github.com/grahambrereton-form3/tilt@v0.10.18/internal/cloud/cloud_username_manager_test.go (about) 1 package cloud 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/require" 10 11 "github.com/windmilleng/tilt/internal/feature" 12 "github.com/windmilleng/tilt/internal/store" 13 "github.com/windmilleng/tilt/internal/testutils" 14 "github.com/windmilleng/tilt/internal/testutils/httptest" 15 ) 16 17 const testCloudAddress = "tiltcloud.example.com" 18 19 func TestLongGet(t *testing.T) { 20 f := newCloudUsernameManagerTestFixture(t) 21 22 f.httpClient.SetResponse(`{"foo": "bar"}`) 23 f.Run(func(state *store.EngineState) { 24 state.WaitingForTiltCloudUsernamePostRegistration = true 25 }) 26 27 f.waitForRequest(fmt.Sprintf("https://%s/api/whoami?wait_for_registration=true", testCloudAddress)) 28 } 29 30 type cloudUsernameManagerTestFixture struct { 31 um *CloudUsernameManager 32 httpClient *httptest.FakeClient 33 st *store.Store 34 ctx context.Context 35 t *testing.T 36 } 37 38 func newCloudUsernameManagerTestFixture(t *testing.T) *cloudUsernameManagerTestFixture { 39 st, _ := store.NewStoreForTesting() 40 41 httpClient := httptest.NewFakeClient() 42 43 ctx, _, _ := testutils.CtxAndAnalyticsForTest() 44 45 return &cloudUsernameManagerTestFixture{ 46 st: st, 47 httpClient: httpClient, 48 um: NewUsernameManager(httpClient), 49 ctx: ctx, 50 t: t, 51 } 52 } 53 54 func (f *cloudUsernameManagerTestFixture) Run(mutateState func(state *store.EngineState)) { 55 state := f.st.LockMutableStateForTesting() 56 state.Features = make(map[string]bool) 57 state.Features[feature.Snapshots] = true 58 state.CloudAddress = testCloudAddress 59 mutateState(state) 60 f.st.UnlockMutableState() 61 f.um.OnChange(f.ctx, f.st) 62 } 63 64 func (f *cloudUsernameManagerTestFixture) waitForRequest(expectedURL string) { 65 timeout := time.After(time.Second) 66 for { 67 urls := f.httpClient.RequestURLs() 68 if len(urls) > 1 { 69 f.t.Fatalf("%T made more than one http request! requests: %v", f.um, urls) 70 } else if len(urls) == 1 { 71 require.Equal(f.t, expectedURL, urls[0]) 72 return 73 } else { 74 select { 75 case <-timeout: 76 f.t.Fatalf("timed out waiting for %T to make http request", f.um) 77 case <-time.After(10 * time.Millisecond): 78 } 79 } 80 } 81 }