(t *testing.T)
| 11 | ) |
| 12 | |
| 13 | func TestAuthService_CreateAuth(t *testing.T) { |
| 14 | // Ensure we can create a new auth object and associated user. |
| 15 | t.Run("OK", func(t *testing.T) { |
| 16 | db := MustOpenDB(t) |
| 17 | defer MustCloseDB(t, db) |
| 18 | s := sqlite.NewAuthService(db) |
| 19 | |
| 20 | expiry := time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) |
| 21 | auth := &wtf.Auth{ |
| 22 | Source: wtf.AuthSourceGitHub, |
| 23 | SourceID: "SOURCEID", |
| 24 | AccessToken: "ACCESS", |
| 25 | RefreshToken: "REFRESH", |
| 26 | Expiry: &expiry, |
| 27 | User: &wtf.User{ |
| 28 | Name: "jill", |
| 29 | Email: "jill@gmail.com", |
| 30 | }, |
| 31 | } |
| 32 | |
| 33 | // Create new auth object & ensure ID and timestamps are returned. |
| 34 | if err := s.CreateAuth(context.Background(), auth); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } else if got, want := auth.ID, 1; got != want { |
| 37 | t.Fatalf("ID=%v, want %v", got, want) |
| 38 | } else if auth.CreatedAt.IsZero() { |
| 39 | t.Fatal("expected created at") |
| 40 | } else if auth.UpdatedAt.IsZero() { |
| 41 | t.Fatal("expected updated at") |
| 42 | } |
| 43 | |
| 44 | // Fetch auth from database & compare. |
| 45 | if other, err := s.FindAuthByID(context.Background(), 1); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } else if !reflect.DeepEqual(auth, other) { |
| 48 | t.Fatalf("mismatch: %#v != %#v", auth, other) |
| 49 | } |
| 50 | |
| 51 | // Fetching user should return auths. |
| 52 | if user, err := sqlite.NewUserService(db).FindUserByID(context.Background(), 1); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } else if len(user.Auths) != 1 { |
| 55 | t.Fatal("expected auths") |
| 56 | } else if auth := user.Auths[0]; auth.ID != 1 { |
| 57 | t.Fatalf("unexpected auth: %#v", auth) |
| 58 | } |
| 59 | }) |
| 60 | |
| 61 | // Ensure that a blank source field returns an error. |
| 62 | t.Run("ErrSourceRequired", func(t *testing.T) { |
| 63 | db := MustOpenDB(t) |
| 64 | defer MustCloseDB(t, db) |
| 65 | if err := sqlite.NewAuthService(db).CreateAuth(context.Background(), &wtf.Auth{ |
| 66 | User: &wtf.User{Name: "NAME"}, |
| 67 | }); err == nil { |
| 68 | t.Fatal("expected error") |
| 69 | } else if wtf.ErrorCode(err) != wtf.EINVALID || wtf.ErrorMessage(err) != `Source required.` { |
| 70 | t.Fatalf("unexpected error: %#v", err) |
nothing calls this directly
no test coverage detected