|
| 1 | +package hardwaretest |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + root "github.com/equinix/metal-cli/internal/cli" |
| 14 | + outputPkg "github.com/equinix/metal-cli/internal/outputs" |
| 15 | + "github.com/equinix/metal-cli/internal/twofa" |
| 16 | + "github.com/spf13/cobra" |
| 17 | +) |
| 18 | + |
| 19 | +var mockOtpUri = "otpauth://totp/foo" |
| 20 | + |
| 21 | +func setupMock() *root.Client { |
| 22 | + mockAPI := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 23 | + var responseBody string |
| 24 | + if r.URL.Path == "/user/otp/sms/receive" { |
| 25 | + w.WriteHeader(http.StatusNoContent) |
| 26 | + } else if r.URL.Path == "/user/otp/app/receive" { |
| 27 | + w.Header().Add("Content-Type", "application/json") |
| 28 | + responseBody = fmt.Sprintf(`{"otp_uri": "%v"}`, mockOtpUri) |
| 29 | + |
| 30 | + } else { |
| 31 | + responseBody = fmt.Sprintf("no mock for endpoint %v", r.URL.Path) |
| 32 | + w.WriteHeader(http.StatusNotImplemented) |
| 33 | + } |
| 34 | + _, err := w.Write([]byte(responseBody)) |
| 35 | + if err != nil { |
| 36 | + log.Fatalf("Failed to write mock response: %v", err) |
| 37 | + } |
| 38 | + })) |
| 39 | + mockClient := root.NewClient("", mockAPI.URL, "metal") |
| 40 | + return mockClient |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | +func TestCli_Twofa(t *testing.T) { |
| 45 | + subCommand := "2fa" |
| 46 | + // Adjust this response as needed for your tests. |
| 47 | + |
| 48 | + rootClient := setupMock() |
| 49 | + |
| 50 | + type fields struct { |
| 51 | + MainCmd *cobra.Command |
| 52 | + Outputer outputPkg.Outputer |
| 53 | + } |
| 54 | + tests := []struct { |
| 55 | + name string |
| 56 | + fields fields |
| 57 | + want *cobra.Command |
| 58 | + cmdFunc func(*testing.T, *cobra.Command) |
| 59 | + }{ |
| 60 | + { |
| 61 | + name: "receive sms", |
| 62 | + fields: fields{ |
| 63 | + MainCmd: twofa.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), |
| 64 | + Outputer: outputPkg.Outputer(&outputPkg.Standard{}), |
| 65 | + }, |
| 66 | + want: &cobra.Command{}, |
| 67 | + cmdFunc: func(t *testing.T, c *cobra.Command) { |
| 68 | + root := c.Root() |
| 69 | + root.SetArgs([]string{subCommand, "receive", "-s"}) |
| 70 | + rescueStdout := os.Stdout |
| 71 | + r, w, _ := os.Pipe() |
| 72 | + os.Stdout = w |
| 73 | + if err := root.Execute(); err != nil { |
| 74 | + t.Error(err) |
| 75 | + } |
| 76 | + w.Close() |
| 77 | + out, _ := io.ReadAll(r) |
| 78 | + |
| 79 | + os.Stdout = rescueStdout |
| 80 | + if !strings.Contains(string(out[:]), "SMS token sent to your phone") { |
| 81 | + t.Error("expected output to include 'SMS token sent to your phone'.") |
| 82 | + } |
| 83 | + }, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "receive app", |
| 87 | + fields: fields{ |
| 88 | + MainCmd: twofa.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), |
| 89 | + Outputer: outputPkg.Outputer(&outputPkg.Standard{}), |
| 90 | + }, |
| 91 | + want: &cobra.Command{}, |
| 92 | + cmdFunc: func(t *testing.T, c *cobra.Command) { |
| 93 | + root := c.Root() |
| 94 | + root.SetArgs([]string{subCommand, "receive", "-a"}) |
| 95 | + rescueStdout := os.Stdout |
| 96 | + r, w, _ := os.Pipe() |
| 97 | + os.Stdout = w |
| 98 | + if err := root.Execute(); err != nil { |
| 99 | + t.Error(err) |
| 100 | + } |
| 101 | + w.Close() |
| 102 | + out, _ := io.ReadAll(r) |
| 103 | + |
| 104 | + os.Stdout = rescueStdout |
| 105 | + if !strings.Contains(string(out[:]), mockOtpUri) { |
| 106 | + t.Errorf("expected output to include %v", mockOtpUri) |
| 107 | + } |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | + for _, tt := range tests { |
| 112 | + t.Run(tt.name, func(t *testing.T) { |
| 113 | + rootCmd := rootClient.NewCommand() |
| 114 | + rootCmd.AddCommand(tt.fields.MainCmd) |
| 115 | + tt.cmdFunc(t, tt.fields.MainCmd) |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments