|
| 1 | +// Copyright 2019 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package auth |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "net/http" |
| 22 | + "reflect" |
| 23 | + "testing" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + testActionLink = "https://test.link" |
| 28 | + testActionLinkFormat = `{"oobLink": %q}` |
| 29 | + testEmail = "user@domain.com" |
| 30 | +) |
| 31 | + |
| 32 | +var testActionLinkResponse = []byte(fmt.Sprintf(testActionLinkFormat, testActionLink)) |
| 33 | +var testActionCodeSettings = &ActionCodeSettings{ |
| 34 | + URL: "https://example.dynamic.link", |
| 35 | + HandleCodeInApp: true, |
| 36 | + DynamicLinkDomain: "custom.page.link", |
| 37 | + IOSBundleID: "com.example.ios", |
| 38 | + AndroidPackageName: "com.example.android", |
| 39 | + AndroidInstallApp: true, |
| 40 | + AndroidMinimumVersion: "6", |
| 41 | +} |
| 42 | +var testActionCodeSettingsMap = map[string]interface{}{ |
| 43 | + "continueUrl": "https://example.dynamic.link", |
| 44 | + "canHandleCodeInApp": true, |
| 45 | + "dynamicLinkDomain": "custom.page.link", |
| 46 | + "iOSBundleId": "com.example.ios", |
| 47 | + "androidPackageName": "com.example.android", |
| 48 | + "androidInstallApp": true, |
| 49 | + "androidMinimumVersion": "6", |
| 50 | +} |
| 51 | +var invalidActionCodeSettings = []struct { |
| 52 | + name string |
| 53 | + settings *ActionCodeSettings |
| 54 | + want string |
| 55 | +}{ |
| 56 | + { |
| 57 | + "no-url", |
| 58 | + &ActionCodeSettings{}, |
| 59 | + "URL must not be empty", |
| 60 | + }, |
| 61 | + { |
| 62 | + "malformed-url", |
| 63 | + &ActionCodeSettings{ |
| 64 | + URL: "not a url", |
| 65 | + }, |
| 66 | + `malformed url string: "not a url"`, |
| 67 | + }, |
| 68 | + { |
| 69 | + "no-android-package-1", |
| 70 | + &ActionCodeSettings{ |
| 71 | + URL: "https://example.dynamic.link", |
| 72 | + AndroidInstallApp: true, |
| 73 | + }, |
| 74 | + "Android package name is required when specifying other Android settings", |
| 75 | + }, |
| 76 | + { |
| 77 | + "no-android-package-2", |
| 78 | + &ActionCodeSettings{ |
| 79 | + URL: "https://example.dynamic.link", |
| 80 | + AndroidMinimumVersion: "6", |
| 81 | + }, |
| 82 | + "Android package name is required when specifying other Android settings", |
| 83 | + }, |
| 84 | +} |
| 85 | + |
| 86 | +func TestEmailVerificationLink(t *testing.T) { |
| 87 | + s := echoServer(testActionLinkResponse, t) |
| 88 | + defer s.Close() |
| 89 | + |
| 90 | + link, err := s.Client.EmailVerificationLink(context.Background(), testEmail) |
| 91 | + if err != nil { |
| 92 | + t.Fatal(err) |
| 93 | + } |
| 94 | + if link != testActionLink { |
| 95 | + t.Errorf("EmailVerificationLink() = %q; want = %q", link, testActionLink) |
| 96 | + } |
| 97 | + |
| 98 | + want := map[string]interface{}{ |
| 99 | + "requestType": "VERIFY_EMAIL", |
| 100 | + "email": testEmail, |
| 101 | + "returnOobLink": true, |
| 102 | + } |
| 103 | + if err := checkActionLinkRequest(want, s); err != nil { |
| 104 | + t.Fatal(err) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestEmailVerificationLinkWithSettings(t *testing.T) { |
| 109 | + s := echoServer(testActionLinkResponse, t) |
| 110 | + defer s.Close() |
| 111 | + |
| 112 | + link, err := s.Client.EmailVerificationLinkWithSettings(context.Background(), testEmail, testActionCodeSettings) |
| 113 | + if err != nil { |
| 114 | + t.Fatal(err) |
| 115 | + } |
| 116 | + if link != testActionLink { |
| 117 | + t.Errorf("EmailVerificationLinkWithSettings() = %q; want = %q", link, testActionLink) |
| 118 | + } |
| 119 | + |
| 120 | + want := map[string]interface{}{ |
| 121 | + "requestType": "VERIFY_EMAIL", |
| 122 | + "email": testEmail, |
| 123 | + "returnOobLink": true, |
| 124 | + } |
| 125 | + for k, v := range testActionCodeSettingsMap { |
| 126 | + want[k] = v |
| 127 | + } |
| 128 | + if err := checkActionLinkRequest(want, s); err != nil { |
| 129 | + t.Fatal(err) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestEmailVerificationLinkNoEmail(t *testing.T) { |
| 134 | + client := &Client{} |
| 135 | + _, err := client.EmailVerificationLink(context.Background(), "") |
| 136 | + if err == nil { |
| 137 | + t.Errorf("EmailVerificationLink('') = nil; want error") |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func TestEmailVerificationLinkInvalidSettings(t *testing.T) { |
| 142 | + client := &Client{} |
| 143 | + for _, tc := range invalidActionCodeSettings { |
| 144 | + _, err := client.EmailVerificationLinkWithSettings(context.Background(), testEmail, tc.settings) |
| 145 | + if err == nil || err.Error() != tc.want { |
| 146 | + t.Errorf("EmailVerificationLinkWithSettings(%q) = %v; want = %q", tc.name, err, tc.want) |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func TestEmailVerificationLinkError(t *testing.T) { |
| 152 | + cases := map[string]func(error) bool{ |
| 153 | + "UNAUTHORIZED_DOMAIN": IsUnauthorizedContinueURI, |
| 154 | + "INVALID_DYNAMIC_LINK_DOMAIN": IsInvalidDynamicLinkDomain, |
| 155 | + } |
| 156 | + s := echoServer(testActionLinkResponse, t) |
| 157 | + defer s.Close() |
| 158 | + s.Client.httpClient.RetryConfig = nil |
| 159 | + s.Status = http.StatusInternalServerError |
| 160 | + |
| 161 | + for code, check := range cases { |
| 162 | + resp := fmt.Sprintf(`{"error": {"message": %q}}`, code) |
| 163 | + s.Resp = []byte(resp) |
| 164 | + _, err := s.Client.EmailVerificationLink(context.Background(), testEmail) |
| 165 | + if err == nil || !check(err) { |
| 166 | + t.Errorf("EmailVerificationLink(%q) = %v; want = %q", code, err, serverError[code]) |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func checkActionLinkRequest(want map[string]interface{}, s *mockAuthServer) error { |
| 172 | + var got map[string]interface{} |
| 173 | + if err := json.Unmarshal(s.Rbody, &got); err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + if !reflect.DeepEqual(got, want) { |
| 177 | + return fmt.Errorf("EmailVerificationLink() request = %#v; want = %#v", got, want) |
| 178 | + } |
| 179 | + return nil |
| 180 | +} |
0 commit comments