|
| 1 | +package nucleusdb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/uuid" |
| 8 | + "github.com/jackc/pgx/v5/pgtype" |
| 9 | + db_queries "github.com/nucleuscloud/neosync/backend/gen/go/db" |
| 10 | + "github.com/stretchr/testify/mock" |
| 11 | + "github.com/zeebo/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func Test_CreateAccountApiKey(t *testing.T) { |
| 15 | + dbtxMock := NewMockDBTX(t) |
| 16 | + querierMock := db_queries.NewMockQuerier(t) |
| 17 | + mockTx := new(MockTx) |
| 18 | + |
| 19 | + service := New(dbtxMock, querierMock) |
| 20 | + dbtxMock.On("Begin", mock.Anything).Return(mockTx, nil) |
| 21 | + mockTx.On("Commit", mock.Anything).Return(nil) |
| 22 | + mockTx.On("Rollback", mock.Anything).Return(nil) |
| 23 | + user := db_queries.NeosyncApiUser{ |
| 24 | + ID: newPgUuid(t), |
| 25 | + UserType: 1, |
| 26 | + } |
| 27 | + querierMock.On("CreateMachineUser", mock.Anything, mock.Anything). |
| 28 | + Return(user, nil) |
| 29 | + apiKeyUuid := newPgUuid(t) |
| 30 | + querierMock.On("CreateAccountApiKey", mock.Anything, mock.Anything, mock.Anything). |
| 31 | + Return(db_queries.NeosyncApiAccountApiKey{ |
| 32 | + ID: apiKeyUuid, |
| 33 | + }, nil) |
| 34 | + |
| 35 | + newApiKey, err := service.CreateAccountApikey( |
| 36 | + context.Background(), |
| 37 | + &CreateAccountApiKeyRequest{}, |
| 38 | + ) |
| 39 | + assert.NoError(t, err) |
| 40 | + assert.NotNil(t, newApiKey) |
| 41 | + assert.Equal(t, newApiKey.ID, apiKeyUuid) |
| 42 | +} |
| 43 | + |
| 44 | +func newPgUuid(t *testing.T) pgtype.UUID { |
| 45 | + t.Helper() |
| 46 | + newuuid := uuid.NewString() |
| 47 | + val, err := ToUuid(newuuid) |
| 48 | + assert.NoError(t, err) |
| 49 | + return val |
| 50 | +} |
0 commit comments