func TestEditUserProfileError(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") ln.SetOutput(ioutil.Discard, "test_logger") someProfile := client.UserProfile{ UserID: 23, Phone: "123-123-1234", Website: "www.google.com", FirstName: "Homer", LastName: "Simpson", Address1: "12345 Seasame St.", City: "Springfield", State: "IL", Zip: "11252", Country: "USA", Company: "Simpsons", } fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("editUserProfile", func(params url.Values) (interface{}, error) { return 0, errors.New("some serious error happened") }) adaptor := New(fakeClient) success, err := adaptor.EditUserProfile(&someProfile) require.NotNil(t, err) assert.False(t, success) }
func TestGetSubusers(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") expected := []client.Subuser{ { ID: 1, Username: "******", Email: "*****@*****.**", }, { ID: 2, Username: "******", Email: "*****@*****.**", }, } fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("getSubusers", func(params url.Values) (interface{}, error) { return expected, nil }) adaptor := New(fakeClient) actual, adaptorError := adaptor.GetSubusers(&client.SubuserRequest{UserID: 180}) assert.Nil(t, adaptorError) if assert.NotNil(t, actual) { assert.Equal(t, len(expected), len(actual)) for i, _ := range actual { assert.Equal(t, expected[i].ID, actual[i].ID) assert.Equal(t, expected[i].Username, actual[i].Username) assert.Equal(t, expected[i].Email, actual[i].Email) } } }
func TestAddFiltersInvalid(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") user := client.Signup{ Username: "******", UserID: 1, Password: "******", Email: "*****@*****.**", } fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("enableUserFilter", func(params url.Values) (interface{}, error) { return 1, nil }) expectedApidError := fmt.Errorf("apid internal server error") fakeClient.RegisterFunction("addUserFilters", func(params url.Values) (interface{}, error) { return 0, expectedApidError }) adaptor := New(fakeClient) errs := adaptor.AddFilters(user) assert.Len(t, errs, 4) assert.Equal(t, errs[0], expectedApidError) assert.Equal(t, errs[1], expectedApidError) assert.Equal(t, errs[2], expectedApidError) assert.Equal(t, errs[3], expectedApidError) }
func TestGetSignupPackageInfoValid(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") // Apid return free package fakeClient := clientfakes.NewFakeClient() expectedResponse := client.PackageRecord{ID: 1, IsFree: 1, PackageGroupID: 1, Credits: 12000} fakeClient.RegisterFunction("get", func(params url.Values) (interface{}, error) { return []client.PackageRecord{expectedResponse}, nil }) adaptor := New(fakeClient) freePackage := adaptor.GetSignupPackageInfo("abc") assert.Equal(t, expectedResponse, freePackage) // Apid return paid package, then our function will return 12k Free package fakeClient = clientfakes.NewFakeClient() fakeClient.RegisterFunction("get", func(params url.Values) (interface{}, error) { return []client.PackageRecord{client.PackageRecord{ID: 1, IsFree: 0, PackageGroupID: 1, Credits: 12000}}, nil }) adaptor = New(fakeClient) freePackage = adaptor.GetSignupPackageInfo("abc") assert.Equal(t, client.PackageRecord{ID: FreePackageID, IsFree: 1, PackageGroupID: FreePackageGroupID, Credits: FreeAccountCreditsLimits}, freePackage) }
func TestAddUserPackageForSubuser(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() addPackageSpy := fakeClient.RegisterFunction("add", func(param url.Values) (interface{}, error) { return "success", nil }) adaptor := New(fakeClient) err := adaptor.AddUserPackage( client.Signup{ UserID: 180, ResellerID: 888, UserPackageStatus: 444, }) assert.Len(t, err, 0) //verify subuser params are removed params := addPackageSpy.CalledParams["values"][0] exists := strings.Contains(params, "package_id") assert.False(t, exists, "package id should be null") exists = strings.Contains(params, "package_group_id") assert.False(t, exists, "package group id should be null") exists = strings.Contains(params, "package_status") assert.False(t, exists, "package status should be null") //verify subuser params is modified re := regexp.MustCompile(`{"status":"(\d*)"}`) matches := re.FindAllStringSubmatch(params, -1) status := matches[0][1] exists = strings.Contains(params, "status") assert.True(t, exists, "status is passed through") assert.Equal(t, "444", status, "status should be pass through") //default is 1 = SendGrid Free }
func TestEditUserNoUpdate(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") // When a call is made to update a user, but there are no changes in the db // (If we are editing to the same info the user already has in the db) userID := 23 var actualParams url.Values fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("editUser", func(params url.Values) (interface{}, error) { actualParams = params return 0, nil }) adaptor := New(fakeClient) request := &client.User{ ID: userID, AccountOwnerID: 123, Username: "******", Email: "*****@*****.**", } actual, adaptorErr := adaptor.EditUser(request) assert.NoError(t, adaptorErr) assert.Equal(t, actualParams, url.Values{"userid": []string{strconv.Itoa(userID)}, "reseller_id": []string{"123"}, "username": []string{"jake_the_dog"}, "email": []string{"*****@*****.**"}}) assert.True(t, actual) }
func TestAddFiltersValid(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") user := client.Signup{ Username: "******", UserID: 1, Password: "******", Email: "*****@*****.**", } // filter already enabled fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("enableUserFilter", func(params url.Values) (interface{}, error) { return 1, nil }) // no errors, but register one row updated fakeClient.RegisterFunction("addUserFilters", func(params url.Values) (interface{}, error) { return 1, nil }) adaptor := New(fakeClient) errs := adaptor.AddFilters(user) assert.Len(t, errs, 0) }
func TestGetEmptyUser(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") userID := 23 expected := User{ID: userID} fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("getUserInfo", func(params url.Values) (interface{}, error) { userIDFromParams, _ := strconv.Atoi(params.Get("userid")) switch userIDFromParams { case userID: return expected, nil } return nil, fmt.Errorf("not a valid call") }) adaptor := New(fakeClient) actual, adaptorErr := adaptor.GetUser(userID) require.Nil(t, adaptorErr) require.NotNil(t, actual) assert.Equal(t, expected.ID, actual.ID) assert.Equal(t, expected.Username, actual.Username) assert.Equal(t, expected.AccountOwnerID, actual.AccountOwnerID) assert.Equal(t, expected.AccountID, actual.AccountID) }
func TestCreateUserError(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") expectedNewUserID := 1234 // value to be passed into apid expectedUsername := "******" // expected output from apidadaptor expectedErr := adaptor.NewError("error when adding user") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("addUser", func(params url.Values) (interface{}, error) { username := params.Get("username") if username == expectedUsername { return 0, fmt.Errorf("some apid error") } return expectedNewUserID, nil }) adaptor := New(fakeClient) _, adaptorErr := adaptor.CreateUser( client.Signup{ Username: expectedUsername, Email: "*****@*****.**", Password: "******", }) if assert.NotNil(t, adaptorErr, adaptorErr.Error()) { assert.Equal(t, expectedErr.Error(), adaptorErr.Error()) assert.Equal(t, expectedErr.SuggestedStatusCode, adaptorErr.SuggestedStatusCode) } }
func TestUpdateURLMailDomainFailure(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() var actualParams url.Values err := errors.New("error") fakeClient.RegisterFunction("update", func(params url.Values) (interface{}, error) { actualParams = params return nil, err }) adaptor := New(fakeClient) testUserID := 1 testUrlDomain := fmt.Sprintf("u%d.ct.sendgrid.net", testUserID) testMailDomain := "sendgrid.net" updateErr := adaptor.UpdateURLMailDomain(testUserID) assert.NotNil(t, updateErr) assert.Equal(t, actualParams.Get("tableName"), "user") assert.Equal(t, actualParams.Get("where"), fmt.Sprintf(`{"id" : "%d"}`, testUserID)) assert.Equal(t, actualParams.Get("values"), fmt.Sprintf(`[{"url_domain": "%s"},{"mail_domain": "%s"}]`, testUrlDomain, testMailDomain)) }
func TestAddIPGroup(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") missingUserIDError := errors.New("missing userid") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("addUserIpGroup", func(param url.Values) (interface{}, error) { if param.Get("userid") == "0" { return 0, missingUserIDError } return 1, nil }) adaptor := New(fakeClient) // test good case err := adaptor.AddIPGroup( client.Signup{ UserID: 180, }) assert.Len(t, err, 0) // test missing user error err = adaptor.AddIPGroup(client.Signup{}) assert.Len(t, err, 1) assert.Equal(t, missingUserIDError, err[0]) }
func TestEditUser(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") userID := 23 var actualParams url.Values fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("editUser", func(params url.Values) (interface{}, error) { actualParams = params return 1, nil }) adaptor := New(fakeClient) request := &client.User{ ID: userID, AccountOwnerID: 123, Username: "******", Email: "*****@*****.**", } actual, adaptorErr := adaptor.EditUser(request) assert.NoError(t, adaptorErr) assert.Equal(t, actualParams, url.Values{"userid": []string{strconv.Itoa(userID)}, "reseller_id": []string{"123"}, "username": []string{"jake_the_dog"}, "email": []string{"*****@*****.**"}}) assert.True(t, actual) }
func TestInsertDeactivationReaonsApidErr(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() expectedValidReasons := []userChurnReason{ userChurnReason{ID: 1, Reason: "reason_a"}, userChurnReason{ID: 2, Reason: "reason_b"}, } fakeClient.RegisterFunction("get", func(params url.Values) (interface{}, error) { if params.Get("tableName") == "competitors" { return []competitor{competitor{ID: 3}}, nil } return expectedValidReasons, nil }) fakeClient.RegisterFunction("add", func(params url.Values) (interface{}, error) { if params.Get("tableName") == "competitors" { return "success", nil } return "", errors.New("some apid error adding to user_churn") }) adaptor := New(fakeClient) reason := "reason_a" moving := true inHouse := true adaptorErr := adaptor.InsertDeactivationReason(1800, reason, moving, inHouse, "new provider", "notes on why") require.NotNil(t, adaptorErr, "should error") }
func TestGetUserByUsernameSlaveFailMasterSuccess(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") username := "******" bad := User{ID: 0} expected := User{ID: 123, Username: username, AccountOwnerID: 4000, AccountID: "iamanaccountid"} fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("getUserInfo", func(params url.Values) (interface{}, error) { return bad, nil }) fakeClient.RegisterFunction("getUserInfoMaster", func(params url.Values) (interface{}, error) { return expected, nil }) adaptor := New(fakeClient) actual, clientErr := adaptor.GetUserByUsername(username) assert.Nil(t, clientErr) require.NotNil(t, actual) assert.Equal(t, expected.ID, actual.ID) assert.Equal(t, expected.Username, actual.Username) assert.Equal(t, expected.AccountOwnerID, actual.AccountOwnerID) assert.Equal(t, expected.AccountID, actual.AccountID) }
func TestGetUserHolds(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") var userIDFromParams int expected := UserHolds{ "10": "test", "101": "test2", } fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("getUserHolds", func(params url.Values) (interface{}, error) { userIDFromParams, _ = strconv.Atoi(params.Get("userid")) return expected, nil }) adaptor := New(fakeClient) actual, err := adaptor.GetUserHolds(23) require.Nil(t, err) require.NotNil(t, actual) assert.Equal(t, actual, expected) assert.Equal(t, userIDFromParams, 23) }
func TestGetUserProfileUserNotFound(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") ln.SetOutput(ioutil.Discard, "test_logger") someOtherUserID := 24 fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("getUserProfile", func(params url.Values) (interface{}, error) { return nil, errors.New("some serious error happened") }) adaptor := New(fakeClient) actual, err := adaptor.GetUserProfile(someOtherUserID) require.NotNil(t, err) require.Nil(t, actual) }
func TestEditUserProfile(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") expected := client.UserProfile{ UserID: 23, Phone: "123-123-1234", Website: "www.google.com", FirstName: "Homer", LastName: "Simpson", Address1: "12345 Seasame St.", City: "Springfield", State: "IL", Zip: "11252", Country: "USA", Company: "Simpsons", } toModify := client.UserProfile{} fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("editUserProfile", func(params url.Values) (interface{}, error) { userID, _ := strconv.Atoi(params.Get("user_id")) toModify.UserID = userID toModify.Phone = params.Get("phone") toModify.Website = params.Get("website") toModify.FirstName = params.Get("first_name") toModify.LastName = params.Get("last_name") toModify.Address1 = params.Get("address") toModify.City = params.Get("city") toModify.State = params.Get("state") toModify.Zip = params.Get("zip") toModify.Country = params.Get("country") toModify.Company = params.Get("company") return 1, nil }) adaptor := New(fakeClient) success, err := adaptor.EditUserProfile(&expected) require.Nil(t, err) assert.True(t, success) assert.Equal(t, expected.Phone, toModify.Phone) assert.Equal(t, expected.Website, toModify.Website) assert.Equal(t, expected.FirstName, toModify.FirstName) assert.Equal(t, expected.LastName, toModify.LastName) assert.Equal(t, expected.Address1, toModify.Address1) assert.Equal(t, expected.City, toModify.City) assert.Equal(t, expected.State, toModify.State) assert.Equal(t, expected.Zip, toModify.Zip) assert.Equal(t, expected.Country, toModify.Country) assert.Equal(t, expected.Company, toModify.Company) }
func TestAddTalonFingerprintSuccessError(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("add", func(params url.Values) (interface{}, error) { return "", errors.New("some error") }) adaptor := New(fakeClient) ok, adaptorErr := adaptor.AddUserFingerprint(TalonFingerprint{}) assert.NotNil(t, adaptorErr, "error should have occured") assert.False(t, ok, "fingerprint errored") }
func TestAddTalonFingerprintSuccessOnDuplicate(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("add", func(params url.Values) (interface{}, error) { return "", errors.New("key exists") }) adaptor := New(fakeClient) ok, adaptorErr := adaptor.AddUserFingerprint(TalonFingerprint{}) require.Nil(t, adaptorErr, "no error should have occured") assert.True(t, ok, "fingerprint added") }
func TestDeactivateUserPackageError(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("update", func(params url.Values) (interface{}, error) { return "", errors.New("some apid error for downgrade user package") }) adaptor := New(fakeClient) adaptorErr := adaptor.DeactivateUserPackage(1800) require.NotNil(t, adaptorErr, "should error") }
func TestDeactivateUserPackageSuccess(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("update", func(params url.Values) (interface{}, error) { return "success", nil }) adaptor := New(fakeClient) adaptorErr := adaptor.DeactivateUserPackage(1800) require.Nil(t, adaptorErr, fmt.Sprintf("got %#v, but should not error", adaptorErr.Error())) }
func TestUpdateUserBISSomeAPIDErr(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("update", func(params url.Values) (interface{}, error) { return "", errors.New("some apid err") }) adaptor := New(fakeClient) err := adaptor.UpdateUserBI(client.Provision{}) require.NotNil(t, err, "should err") }
func TestUpdateUserBIWrongResult(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("update", func(params url.Values) (interface{}, error) { return "some unexpected result", nil }) adaptor := New(fakeClient) err := adaptor.UpdateUserBI(client.Provision{}) require.NotNil(t, err, "should err") }
func TestGetSignupPackageInfoInvalid(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("get", func(params url.Values) (interface{}, error) { return nil, fmt.Errorf("apid internal server error") }) adaptor := New(fakeClient) freePackage := adaptor.GetSignupPackageInfo("abc") assert.Equal(t, client.PackageRecord{ID: FreePackageID, IsFree: 1, PackageGroupID: FreePackageGroupID, Credits: FreeAccountCreditsLimits}, freePackage) }
func TestInsertDeactivationReaonsSuccess(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") addMethodCalled := false firstCompetitorGetCall := true fakeClient := clientfakes.NewFakeClient() expectedValidReasons := []userChurnReason{ userChurnReason{ID: 1, Reason: "reason_a"}, userChurnReason{ID: 2, Reason: "reason_b"}, } // in addition to doing a successful deactivation result, // we are also testing that we can insert a new record into // the competitor table. fakeClient.RegisterFunction("get", func(params url.Values) (interface{}, error) { if params.Get("tableName") == "competitors" { if firstCompetitorGetCall { firstCompetitorGetCall = false return []competitor{}, nil } // any subsequent call to get competitors return []competitor{competitor{ID: 3}}, nil } if params.Get("tableName") == "user_churn_reason" { return expectedValidReasons, nil } return "", errors.New("unexpected table called") }) fakeClient.RegisterFunction("add", func(params url.Values) (interface{}, error) { if params.Get("tableName") == "competitors" { addMethodCalled = true t.Log("this should have the new competitor in it:", params.Get("values")) assert.True(t, strings.Contains(params.Get("values"), "Some New Competitor")) return "success", nil } return "success", nil }) adaptor := New(fakeClient) reason := "reason_a" moving := true inHouse := false adaptorErr := adaptor.InsertDeactivationReason(1800, reason, moving, inHouse, "Some New Competitor", "notes on why") assert.True(t, addMethodCalled) require.Nil(t, adaptorErr, fmt.Sprintf("expected no error, got %#v", adaptorErr.Error())) }
func TestFeatureEnabled(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("checkFeatureToggle", func(params url.Values) (interface{}, error) { return true, nil }) adaptor := NewWithRetry(fakeClient, 1*time.Second, 1*time.Second) isEnabled, err := adaptor.IsFeatureEnabled("some_feature") assert.Nil(t, err, "there should be no error") assert.True(t, isEnabled, "feature enabled") }
func TestActivateUser(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") expected := "success" fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("update", func(params url.Values) (interface{}, error) { return expected, nil }) adaptor := New(fakeClient) clientErr := adaptor.ActivateUser(180) assert.Nil(t, clientErr) }
func TestFeatureError(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("checkFeatureToggle", func(params url.Values) (interface{}, error) { return false, errors.New("some apid error") }) adaptor := New(fakeClient) isEnabled, err := adaptor.IsFeatureEnabled("some_feature") assert.False(t, isEnabled, "feature disabled") assert.NotNil(t, err, "there should be an error") }
func TestAddBounceManagementSettings(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("addBounceManagementSettings", func(params url.Values) (interface{}, error) { return 1, nil }) adaptor := New(fakeClient) err := adaptor.AddBounceManagement( client.Signup{ UserID: 180, }) assert.Len(t, err, 0) }
func TestAddBounceManagementFailure(t *testing.T) { ln.SetOutput(ioutil.Discard, "test_logger") fakeClient := clientfakes.NewFakeClient() fakeClient.RegisterFunction("adBounceManagementSettings", func(params url.Values) (interface{}, error) { return 0, errors.New("something went wrong!") }) adaptor := New(fakeClient) err := adaptor.AddBounceManagement( client.Signup{ UserID: 180, }) assert.Len(t, err, 1, "apid generated an expected error") }