Skip to content

Commit

Permalink
feat: submit snap_name in requests to the ratings service
Browse files Browse the repository at this point in the history
  • Loading branch information
d-loose committed Dec 17, 2024
1 parent 6d2386f commit 6bca895
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion packages/app_center/lib/ratings/ratings_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class RatingsModel extends _$RatingsModel {
}

final results = await Future.wait([
_ratings.getRating(snapId),
_ratings.getRating(snapName, snapId),
_ratings.getSnapVotes(snapId),
]);

Expand Down
16 changes: 13 additions & 3 deletions packages/app_center/lib/ratings/ratings_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ class RatingsService {
}
}

Future<ratings.Rating?> getRating(String snapId) async {
Future<ratings.Rating?> getRating(
String snapName,
// TODO: remove snapId once the server doesn't require it anymore
String snapId,
) async {
await _ensureValidToken();
return client.getRating(snapId, _jwt!);
return client.getRating(snapName, snapId, _jwt!);
}

Future<List<ratings.ChartData>> getChart(SnapCategoryEnum category) async {
Expand All @@ -46,7 +50,13 @@ class RatingsService {

Future<void> vote(ratings.Vote vote) async {
await _ensureValidToken();
await client.vote(vote.snapId, vote.snapRevision, vote.voteUp, _jwt!);
await client.vote(
vote.snapName,
vote.snapId,
vote.snapRevision,
vote.voteUp,
_jwt!,
);
}

Future<void> delete() async {
Expand Down
4 changes: 2 additions & 2 deletions packages/app_center/test/ratings_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void main() {
);
final service = RatingsService(mockClient, id: 'myId');

final rating = await service.getRating('firefox');
final rating = await service.getRating('firefox', '1234');
verify(mockClient.authenticate('myId')).called(1);
expect(
rating,
Expand Down Expand Up @@ -101,7 +101,7 @@ void main() {
),
);
verify(mockClient.authenticate('myId')).called(1);
verify(mockClient.vote('7890', 42, true, 'jwt')).called(1);
verify(mockClient.vote('thunderbird', '7890', 42, true, 'jwt')).called(1);
});

test('delete', () async {
Expand Down
4 changes: 2 additions & 2 deletions packages/app_center/test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ MockRatingsService registerMockRatingsService({
List<Vote>? snapVotes,
}) {
final service = MockRatingsService();
when(service.getRating(any)).thenAnswer(
when(service.getRating(any, any)).thenAnswer(
(_) async =>
rating ??
const Rating(
Expand All @@ -322,7 +322,7 @@ MockRatingsClient createMockRatingsClient({
}) {
final client = MockRatingsClient();
when(client.authenticate(any)).thenAnswer((_) async => token ?? '');
when(client.getRating(any, any)).thenAnswer(
when(client.getRating(any, any, any)).thenAnswer(
(_) async =>
rating ??
const Rating(
Expand Down
10 changes: 9 additions & 1 deletion packages/app_center_ratings_client/lib/src/ratings_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,15 @@ class RatingsClient {
}

Future<Rating> getRating(
String snapName,
// TODO: remove snapId once the server doesn't require it anymore
String snapId,
String token,
) async {
final request = app_pb.GetRatingRequest(snapId: snapId);
final request = app_pb.GetRatingRequest(
snapName: snapName,
snapId: snapId,
);
final callOptions =
CallOptions(metadata: {'authorization': 'Bearer $token'});
final grpcResponse = await _appClient.getRating(
Expand All @@ -95,12 +100,15 @@ class RatingsClient {
}

Future<void> vote(
String snapName,
// TODO: remove snapId once the server doesn't require it anymore
String snapId,
int snapRevision,
bool voteUp,
String token,
) async {
final request = user_pb.VoteRequest(
snapName: snapName,
snapId: snapId,
snapRevision: snapRevision,
voteUp: voteUp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void main() {
snapName: snapName,
);
final mockResponse = pb.GetRatingResponse(rating: pbRating);
final request = pb.GetRatingRequest(snapId: snapId);
final request = pb.GetRatingRequest(snapName: snapName, snapId: snapId);
when(
mockAppClient.getRating(
request,
Expand All @@ -125,6 +125,7 @@ void main() {
(_) => MockResponseFuture<pb.GetRatingResponse>(mockResponse),
);
final response = await ratingsClient.getRating(
snapName,
snapId,
token,
);
Expand Down Expand Up @@ -166,10 +167,12 @@ void main() {

test('user votes', () async {
const snapId = 'foo';
const snapName = 'fooName';
const snapRevision = 1;
const voteUp = true;
const token = 'bar';
final request = VoteRequest(
snapName: snapName,
snapId: snapId,
snapRevision: snapRevision,
voteUp: voteUp,
Expand All @@ -182,6 +185,7 @@ void main() {
),
).thenAnswer((_) => MockResponseFuture<Empty>(Empty()));
await ratingsClient.vote(
snapName,
snapId,
snapRevision,
voteUp,
Expand Down

0 comments on commit 6bca895

Please sign in to comment.